Android 사용자 정의 컨트롤 원형 진행 막대 구현

5381 단어 Android진도 표
프로젝트 에서 자주 사용 되 는 원형 진도 바 는 여러 가지 가 있 습 니 다.인터넷 에서 찾 은 사용자 정의 진도 바 는 대부분 봉 인 된 비교적 좋 은 코드 이지 만 초보 자 에 게 불리 합 니 다.현재 본 블 로 그 는 사용자 정의 진도 바 의 효 과 를 한 걸음 한 걸음 실현 하 는 방법 을 알려 드 립 니 다.
일단 효 과 를 보고 그림 처럼...

코드 구현 프로 세 스 Cmain 레이아웃
이 레이아웃 은 간단 한 인용 입 니 다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="    "
 android:onClick="start" />

 <com.example.pb.ProgressView
 android:id="@+id/circleView"
 android:layout_width="100dp"
 android:layout_height="100dp" />

</LinearLayout>
사용자 정의 ProgressView-기본 값 은 그림 의 첫 번 째 효과 입 니 다.

package com.example.pb;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

public class ProgressView extends View {
 int progress = 0;
 private String text="0%";
 private int max = 100;

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

 public ProgressView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }

 public ProgressView(Context context) {
 super(context);
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //     
 Paint paint = new Paint();
 //      
 paint.setAntiAlias(true);
 //       

 //     --Stroke      Fill    FILL_AND_STROKE         
 paint.setStyle(Style.STROKE);
 //      
 paint.setStrokeWidth(2);
 //        
 paint.setColor(Color.RED);
 //       --        ,              
 canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint);
 //        ,             
 paint.setStrokeWidth(3);

 //         ,               
 RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth());
 //     (     )
 paint.setColor(Color.GREEN);
 //  ,         0  ,       ,        ,      
 //true false          ,  true,       ,        
 canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
 //     
 paint.setTextSize(40);
 //      
 paint.setStrokeWidth(1.0f);
 //      -       
 Rect bounds = new Rect();
 //        ,            
 paint.getTextBounds(text, 0, text.length(), bounds);
 paint.setColor(Color.BLACK);
 paint.setStyle(Style.FILL);
 //    ,           
 canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2,
 getMeasuredWidth()/2 + bounds.height() / 2, paint);

 }
 /**
 *             -  100
 * @param max
 */
 public void setMax(int max) {
 this.max = max;
 }
 /**
 *        
 * @param progress
 * @param text
 */
 public void setProgressAndText(int progress, String text) {
 this.progress = progress;
 this.text = text;
 //    
 postInvalidate();
 }

}
하면,만약,만약...

//      
paint.setStyle(Style.FILL);
//  ,         0  ,       ,        ,      
 //true false          ,  true,       ,        
 canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
Activity 에서 코드 C 는 다운로드 과정 을 모 의 하고 효 과 는 마음대로 정의 합 니 다.

package com.example.pb;

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

public class MainActivity extends Activity {

 private ProgressView circleView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 circleView = (ProgressView) findViewById(R.id.circleView);

 }

 int progress = 0;

 public void start(View v) {
 // 1000  
 circleView.setMax(100);
 progress=0;
 new Thread() {
 public void run() {
 while (true) {
  progress = progress + 1;
  String text = progress + "%";
  circleView.setProgressAndText(progress, text);
  try {
  sleep(30);
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  if (progress == 100) {
  break;
  }
 }
 };
 }.start();
 }

}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기