사용자 정의 컨트롤 5: 모조 안지 시장 핸드폰 정리 기능

7939 단어
최근에 안지시장의 핸드폰 정리 기능을 보고 모방해 보려고 했어요.
다음은 원본 그림입니다.
자, 바로 코드에 들어가세요.
attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <attr name="firstColor" format="color"></attr>
    <attr name="sencondColor" format="color"></attr>
    <attr name="cicleWidth" fotmat="dimension"></attr>
    <attr name="speed" format="integer"></attr>
    <attr name="textSize" format="dimension"></attr>
    
    <!--    -->
    <declare-styleable name="CustomProgressBar">
      	<attr name="firstColor"></attr>
      	<attr name="sencondColor"></attr>
      	<attr name="cicleWidth"></attr>
      	<attr name="speed"></attr>
      	<attr name="textSize"></attr>
        
    </declare-styleable>
    
</resources>

CusomProgressBar.java:
package com.example.mycustomwidget_03_4.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.example.mycustomwidget_03_4.R;

public class CusomProgressBar extends View {

	private int firstColor, scondColor;
	private int cicleWidth, speed,textSize;
	private Paint mPaint;
	private int mProgress;
	public static boolean isOK;

	public CusomProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public CusomProgressBar(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public CusomProgressBar(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		//       
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.CustomProgressBar, defStyleAttr, 0);
		int n = a.getIndexCount();

		for (int i = 0; i < n; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.CustomProgressBar_firstColor:
				firstColor = a.getColor(attr, Color.BLUE);
				break;
			case R.styleable.CustomProgressBar_sencondColor:
				scondColor = a.getColor(attr, Color.BLUE);
				break;
			case R.styleable.CustomProgressBar_speed:
				speed = a.getInt(attr, 5);
				break;
			case R.styleable.CustomProgressBar_cicleWidth:
				cicleWidth = a.getDimensionPixelSize(attr, 20);
				break;
			case R.styleable.CustomProgressBar_textSize:
				textSize = a.getDimensionPixelSize(attr, 65);
				break;
			}
		}
		//   
		a.recycle();

	}
	
	
	public void setProgress(int progress) {
		this.mProgress = progress;
		try {
			Thread.sleep(speed);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		postInvalidate();
	}
	

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		init();
				
		int centre = drawArc(canvas);

		drawText(canvas, centre);
		
		drawImg(canvas, centre);
		
	}

	/**
	 *    
	 * @param canvas
	 * @param centre
	 */
	private void drawImg(Canvas canvas, int centre) {
		//      
		if(isOK){
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_broom);
			int bWidth = bitmap.getWidth();
			int bHeight = bitmap.getHeight();
			canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
		}else{
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_ok);
			int bWidth = bitmap.getWidth();
			int bHeight = bitmap.getHeight();
			canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
		}
	}

	
	/**
	 *    
	 * @param canvas
	 * @param centre
	 */
	private void drawText(Canvas canvas, int centre) {
		//        
		mPaint.setColor(Color.WHITE);
		mPaint.setTextSize(textSize);
		//      
		mPaint.setTypeface(Typeface.DEFAULT_BOLD);
		mPaint.setStrokeWidth(0);
		String text = mProgress+"%";
		/**
		 *       
		 */
		float width = mPaint.measureText(text);

		/**
		 *        
		 */
		Rect bounds = new Rect();
		mPaint.getTextBounds(text, 0, 1, bounds);
		//    
		int height = bounds.height();

		canvas.drawText(text, centre - width / 2, centre+height/2, mPaint);
	}

	
	/**
	 *    
	 * @param canvas
	 * @return
	 */
	private int drawArc(Canvas canvas) {
		//   x  
		int centre = getWidth() / 2;
		//   
		int radius = centre - cicleWidth / 2;
		//     
		RectF oval = new RectF(cicleWidth / 2, cicleWidth / 2, getWidth()
				- cicleWidth / 2, getWidth() - cicleWidth / 2);
		//      
		mPaint.setColor(firstColor);
		canvas.drawArc(oval, 135, 270, false, mPaint);
		//      
		mPaint.setColor(scondColor);
		canvas.drawArc(oval, 135, (float) (mProgress*2.7), false, mPaint);
		return centre;
	}

	
	/**
	 * Paint   
	 */
	private void init() {
		mPaint = new Paint();
		mPaint.setStrokeWidth(cicleWidth);
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Style.STROKE);
		//       
		mPaint.setStrokeCap(Paint.Cap.ROUND);
	}

}

다음은 MainActivity입니다.java:
package com.example.mycustomwidget_03_4;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.example.mycustomwidget_03_4.view.CusomProgressBar;

public class MainActivity extends Activity {
	
	private int mProgress;
	private CusomProgressBar pb;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		pb = (CusomProgressBar) findViewById(R.id.pb);
	}

	public void start(View view){
		new Thread() {
			public void run() {
				mProgress = 0;
				pb.isOK = true;
				while (pb.isOK) {
					mProgress++;
					if (mProgress == 100) {
						pb.isOK = false;
					}
					pb.setProgress(mProgress);
				}

			};
		}.start();
	}
	
	
	
	
	
	
	
	

}

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fhl="http://schemas.android.com/apk/res/com.example.mycustomwidget_03_4"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="#32c264"
    android:orientation="vertical"
    >

    <com.example.mycustomwidget_03_4.view.CusomProgressBar
        android:id="@+id/pb"
        fhl:textSize="50sp"
        fhl:firstColor="#33ff00"
        fhl:sencondColor="#ffffff"
        fhl:cicleWidth="20dip"
        fhl:speed="50"
        android:layout_width="200dip"
        android:layout_height="200dip"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="69dp" >

	<Button
	    android:onClick="start"
	    android:text="     "
	    android:layout_width="wrap_content"
	    android:layout_height="50dip"
	    android:layout_alignParentBottom="true"
	    android:layout_centerHorizontal="true"
	    android:gravity="center" />

	</RelativeLayout>
</LinearLayout>

여기서 끝이야.
컴파일 실행 결과는 다음과 같습니다.

원본 다운로드:http://download.csdn.net/detail/yushanfenghailin/8696509

좋은 웹페이지 즐겨찾기