国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義圓形進度條 自定義屬性 單點觸控

2019-11-09 14:33:40
字體:
來源:轉載
供稿:網友
package com.example.administrator.myapplication;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.RectF; import android.os.SystemClock;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;/** * 姓名:戚德水 //// 日期: 2017/2/8. */public class MyView2 extends View {    PRivate  Rect rect;    private float currentX=0;    private float currentY=0;    private  int wColor;    private  int nColor;    private Paint mPaint;    private android.graphics.Canvas canvas;    private RectF rectF;    private float max;    private float progress;    private Boolean aBoolean = true;    private Context context;    private float tag = 0;    private float ballx;    private float bally;    public MyView2(Context context) {        this(context,null);    }    public MyView2(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public MyView2(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        this.context = context;        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView2);        wColor = typedArray.getColor(R.styleable.MyView2_wbackgrount, Color.BLACK);        nColor = typedArray.getColor(R.styleable.MyView2_nbackgrount, Color.GREEN);        max = typedArray.getFloat(R.styleable.MyView2_max,100);        progress = typedArray.getFloat(R.styleable.MyView2_progerss,10);        typedArray.recycle();        mPaint = new Paint();        //抗鋸齒        mPaint.setAntiAlias(true);        //空心 只顯示最外層的線        mPaint.setStyle(Paint.Style.STROKE);        rect = new Rect();    }    @Override    protected void onDraw(final Canvas canvas) {        super.onDraw(canvas);        if (currentX == 0) {            currentX = getWidth() / 2;            currentY = getHeight() / 2;        }        //背景圓環        mPaint.setColor(wColor);        //畫筆寬度        mPaint.setStrokeWidth(50);        canvas.drawCircle(currentX, currentY, 200, mPaint);        //進度條長度        mPaint.setStrokeWidth(30);        mPaint.setColor(nColor);        rectF = new RectF(currentX-200,currentY-200, currentX+200, currentY+200);        canvas.drawArc(rectF,-90,progress/max*360,false,mPaint);        //進度百分比        mPaint.setStrokeWidth(10);        mPaint.setTextSize(100);        int v = (int) (progress / max * 100);        String text = v + "%";        mPaint.getTextBounds(text,0,text.length(),rect);        canvas.drawText(text,currentX-rect.width()/2,currentY+rect.height()/2,mPaint);        float v1 = progress / max * 800;        ballx = currentX - 400 + v1;        bally = currentY - 300;        //總線        mPaint.setColor(wColor);        mPaint.setStrokeWidth(5);        canvas.drawLine(ballx,currentY-300,currentX+400,currentY-300,mPaint);        //進度線        mPaint.setColor(Color.GREEN);        mPaint.setStrokeWidth(5);        canvas.drawLine(currentX-400,currentY-300,currentX-400+ v1,currentY-300,mPaint);        //背景進度原點        mPaint.setColor(Color.GREEN);        mPaint.setStrokeWidth(20);        canvas.drawCircle(ballx, bally, 10, mPaint);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_UP:                tag = 0;                if (aBoolean){                    //給定最大值                    setMax(1098);                    new Thread(new Runnable() {                        @Override                        public void run() {                            while (tag!=max+1){                                SystemClock.sleep(10);                                setProgress(tag);                                //強制繪圖 等于重新執行ondraw()方法                                postInvalidate();                                tag++;                            }                            aBoolean = true;                        }                    }).start();                    aBoolean = false;                }                break;            case MotionEvent.ACTION_DOWN:                Toast.makeText(context, ballx+"|||"+bally, Toast.LENGTH_SHORT).show();                break;            case MotionEvent.ACTION_MOVE:                setCurrentX((int) event.getX());                setCurrentY((int) event.getY());                //強制繪圖 等于重新執行ondraw()方法                postInvalidate();                break;        }        return true;    }    public synchronized float getCurrentX() {        return currentX;    }    public synchronized void setCurrentX(int currentX) {        this.currentX = currentX;    }    public synchronized float getCurrentY(){        return currentY;    }    public synchronized void setCurrentY(int currentY) {        this.currentY = currentY;    }    //提供 ui使用方法    public synchronized float getProgress() {        return progress;    }    public synchronized void setProgress(float progress) {        this.progress = progress;    }    public synchronized float getMax() {        return max;    }    public synchronized void setMax(int max) {        this.max = max;    }}
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyView">        <attr name="textsize" format="float"/>        <attr name="textContext" format="string"/>    </declare-styleable>    <declare-styleable name="MyView2">        <attr name="wbackgrount" format="color"/>        <attr name="nbackgrount" format="color"/>        <attr name="max" format="float"/>        <attr name="progerss" format="float"/>    </declare-styleable></resources>
<com.example.administrator.myapplication.MyView2    android:id="@+id/myview2"    android:layout_width="500dp"    android:layout_height="500dp"    MyView2:wbackgrount="#ff0303"    MyView2:nbackgrount="#0318ff"    android:layout_centerVertical="true"    android:layout_centerHorizontal="true"    android:background="#401717"/>
上一篇:新手看Mockplus

下一篇:Activity大全

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙雅县| 静海县| 玉龙| 庆阳市| 三穗县| 苏尼特右旗| 当雄县| 敦化市| 莎车县| 兖州市| 墨江| 晋宁县| 安龙县| 西畴县| 新野县| 临汾市| 子洲县| 柳州市| 仙游县| 大名县| 华容县| 吕梁市| 黑龙江省| 西畴县| 施甸县| 乡宁县| 克山县| 美姑县| 乡城县| 渭南市| 天长市| 高青县| 邵武市| 台州市| 东乡族自治县| 东港市| 丽江市| 桃江县| 鹤山市| 临颍县| 黔南|