long android.graphics.Paint.getNativeInstance() Android繪圖-仿QQ運動計步

發生這個錯誤百思不得其解,最後發現是繪製調用問題

有三個構造方法,

第一個構造方法時在代碼中創建view的時候可以使用的
而第二個構造方法則是在xml中創建view的時候使用的。

我使用了第三個構造方法,因此Paint爲空,出現這個問題,將其放在第二個構造方法中,問題解決

參考

全部代碼

attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="QQstepView">
        <attr name="outerColor" format="color"/>
        <attr name="innerColor" format="color"/>
        <attr name="borderWidth" format="dimension"/>
        <attr name="stepTextSize" format="dimension"/>
        <attr name="stepTextColor" format="color"/>
    </declare-styleable>

</resources>

QQStepView .class 

package com.example.qq_step;

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.util.AttributeSet;
import android.view.View;

public class QQStepView extends View {

    private int mOuterColor = Color.RED;
    private int mInnerColor = Color.BLUE;
    private int mBorderWidth = 20;
    private int mStepTextSize;
    private int mStepTextColor;
    private Paint mOutPaint, mInnerPaint,mTextPaint;
    private int mStepMax = 0;
    private int mCurrentStep = 0;

    public QQStepView(Context context) {
        super(context,null);
    }

    public QQStepView(Context context, AttributeSet attrs) {
        super(context, attrs,0);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.QQstepView);
        mOuterColor = array.getColor(R.styleable.QQstepView_outerColor,mOuterColor);
        mInnerColor = array.getColor(R.styleable.QQstepView_innerColor,mInnerColor);
        mBorderWidth = (int) array.getDimension(R.styleable.QQstepView_borderWidth,mBorderWidth);
        mStepTextSize =  array.getDimensionPixelSize(R.styleable.QQstepView_stepTextSize,mStepTextSize);
        mStepTextColor = array.getColor(R.styleable.QQstepView_stepTextColor,mStepTextColor);
        array.recycle();//用完以後將array回收
        mOutPaint = new Paint();
        mOutPaint.setAntiAlias(true);
        mOutPaint.setStrokeWidth(mBorderWidth);
        mOutPaint.setColor(mOuterColor);
        mOutPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutPaint.setStyle(Paint.Style.STROKE);

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        mInnerPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mStepTextColor);
        mTextPaint.setTextSize(mStepTextSize);

    }

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

        //1.分析效果
        //2.確定自定義屬性,編寫attrs.xml
        //3.在佈局中使用
        //4.在自定義View中獲取自定義屬性


        //5.onMeasure()

        //6.畫外圓弧、內圓弧、文字
        //7.其它

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //獲取 考慮寬度和高度不一樣的情況
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width>height?height:width,width>height?height:width);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        int center = getWidth()/2;
//        int radius = getWidth()/2 - mBorderWidth/2;
        //6.1 畫外圓弧  順序左上右下
        RectF rectF = new RectF(mBorderWidth/2,mBorderWidth/2,getWidth()-mBorderWidth/2,getHeight()-mBorderWidth/2);
        //從135度開始畫到270度,剩下90度
        canvas.drawArc(rectF,135,270,false,mOutPaint);
        if (mStepMax == 0) {
            return;//防止第一次進入時爲0,引起錯誤
        }
        float sweepAngle = (float) mCurrentStep / mStepMax;
        canvas.drawArc(rectF, 135, sweepAngle * 270, false, mInnerPaint);

        //6.2畫內圓弧


        //6.3 畫文字
        //畫文字
        String stepText = mCurrentStep + "";
        Rect textBounds = new Rect();
        mTextPaint.getTextBounds(stepText, 0, stepText.length(), textBounds);
        int dx = getWidth() / 2 - textBounds.width() / 2;//文字的起始位置
        //基線
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(stepText, dx, baseLine, mTextPaint);
    }


    public synchronized void setStepMax(int stepMax) {
        this.mStepMax = stepMax;
    }

    //    synchronized,防止多線程操作出錯
    public synchronized void setCurrentStep(int currentStep) {
        this.mCurrentStep = currentStep;
        //不斷繪製
        invalidate();
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <com.example.qq_step.QQStepView
        android:id="@+id/step_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:borderWidth="10dp"
        app:innerColor="@color/colorAccent"
        app:outerColor="@color/colorPrimary"
        app:stepTextColor="@color/colorAccent"
        app:stepTextSize="30sp" />

</RelativeLayout>

 mainactivity

package com.example.qq_step;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final QQStepView qqStepView = findViewById(R.id.step_view);
        qqStepView.setStepMax(20000);

        final ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 12450);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) valueAnimator.getAnimatedValue();
                qqStepView.setCurrentStep((int)value);
            }
        });
        qqStepView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                valueAnimator.start();

            }
        });
    }
}

效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章