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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

自定義圓形進(jìn)度條 自定義屬性 單點觸控

2019-11-09 15:41:46
字體:
供稿:網(wǎng)友
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;        }        //背景圓環(huán)        mPaint.setColor(wColor);        //畫筆寬度        mPaint.setStrokeWidth(50);        canvas.drawCircle(currentX, currentY, 200, mPaint);        //進(jìn)度條長度        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);        //進(jìn)度百分比        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);        //進(jìn)度線        mPaint.setColor(Color.GREEN);        mPaint.setStrokeWidth(5);        canvas.drawLine(currentX-400,currentY-300,currentX-400+ v1,currentY-300,mPaint);        //背景進(jìn)度原點        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);                                //強(qiáng)制繪圖 等于重新執(zhí)行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());                //強(qiáng)制繪圖 等于重新執(zhí)行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"/>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋中市| 仲巴县| 河池市| 龙里县| 澄城县| 九江县| 沁源县| 桐梓县| 抚宁县| 福贡县| 镇康县| 北京市| 纳雍县| 慈利县| 铜陵市| 安福县| 保定市| 陆川县| 新余市| 常熟市| 广丰县| 乐陵市| 洞口县| 日喀则市| 青州市| 庆安县| 太仆寺旗| 灯塔市| 曲周县| 平果县| 临江市| 芮城县| 科技| 微山县| 胶州市| 鹤壁市| 赞皇县| 保定市| 乌兰浩特市| 松桃| 北安市|