sinawear

导航

绘制圆形,在指定空间范围内受限的显示效果;

package com.example.com.skills_utf8;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by matengfei on 15/8/10.
 */
public class _64CircleProgress extends ProgressBar{


    Paint paint = new Paint();
    Paint paint_arc = new Paint();
    Paint paint_bitmap = new Paint();

    int radius = 100;
    int stroke_width = 25;
    RectF circle_rect = new RectF();

    //Timer timer = new Timer();
    Handler handler = new Handler();
    int progress = 0;


    Bitmap bitmap_pause;
    Bitmap bitmap_start;




    public _64CircleProgress(Context context, AttributeSet attrs) {
        super(context, attrs);


        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(stroke_width);
        paint.setAntiAlias(true);

        paint_arc.setColor(Color.YELLOW);
        paint_arc.setStyle(Paint.Style.STROKE);
        paint_arc.setStrokeWidth(stroke_width);
        paint_arc.setAntiAlias(true);


        //paint_bitmap

        handler.postDelayed(new MyRunnable(), 1000);



        // 加载暂停,播放按钮
        //BitmapDrawable bitmapdrawable_pause = (BitmapDrawable)getResources().getDrawable(R.drawable.pause_circle, context.getTheme());
        //bitmap_pause = bitmapdrawable_pause.getBitmap();

        bitmap_pause = BitmapFactory.decodeResource(getResources(), R.drawable.pause_circle);
        bitmap_start = BitmapFactory.decodeResource(getResources(), R.drawable.start_circle);

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
       // super.onDraw(canvas);

        if(progress++ >= 360){
            progress = 0;
        }

        circle_rect.set(getWidth() / 2 - radius, getHeight() / 2 - radius, getWidth() / 2 + radius, getHeight() / 2 + radius);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);

        canvas.drawArc(circle_rect, -90, progress, false, paint_arc);

        canvas.drawBitmap(bitmap_pause,
                circle_rect.centerX()-bitmap_pause.getWidth()/2,
                circle_rect.centerY()-bitmap_pause.getHeight()/2,
                paint_bitmap);



    }


    class States{
        public static final int pause = 0;
        public static final int start = 1;
        public static final int stop = 2;

    }



    class MyRunnable implements Runnable {

        @Override
        public void run() {


            invalidate();
            handler.postDelayed(new MyRunnable(), 100);

        }
    }



}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.example.com.skills_utf8._64CircleProgress
        android:id="@+id/progress_cicle"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@null"
        />

</LinearLayout>

posted on 2015-08-10 14:37  sinawear  阅读(180)  评论(0编辑  收藏  举报