编译运行时,报java.lang.ClassCastException,这个是什么原因导致的?下面是我的代码:
package com.example.car_custom_circleview;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// 最多旋转一周,即360°
private final float MAX_ROATE_DEGREE = 1.0f;
private RoundProgressBar mRoundProgressBar1;
private int progress = 0;
// 当前浮点方向
private float mDirection;
// 目标浮点方向
private float mTargetDirection;
// 指南针view
private CompassView mPointer;
// 动画从开始到结束,变化率是一个加速的过程,就是一个动画速率
private AccelerateInterpolator mInterpolator;
private TextView TextViewSpd;
protected final Handler mHandler = new Handler() {
@SuppressLint("HandlerLeak")
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
obtainMessage(1);
int arg1 = msg.arg1;
String spdShow = String.valueOf(arg1);
TextViewSpd.setText(spdShow);
break;
default:
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cricle_progress);
mRoundProgressBar1 = (RoundProgressBar) findViewById(R.id.roundProgressBar1);
// 自定义的指南针view
mPointer = (CompassView) findViewById(R.id.compass_pointer);
// 实例化加速动画对象
mInterpolator = new AccelerateInterpolator();
// 初始化起始方向
mDirection = 0.0f;
// 初始化目标方向
mTargetDirection = 0.0f;
TextViewSpd = (TextView) findViewById(R.id.speed_show);
((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(() -> {
while (true) {
progress += 2;
if (progress > 240) {
progress = 1;
}
mTargetDirection = progress * 360 / 240;
if (mDirection != mTargetDirection) {
// calculate the short routine
float to = mTargetDirection;
if (to - mDirection > 180) {
to -= 360;
} else if (to - mDirection < -180) {
to += 360;
}
// limit the max speed to MAX_ROTATE_DEGREE
float distance = to - mDirection;
if (Math.abs(distance) > MAX_ROATE_DEGREE) {
distance = distance > 0 ? MAX_ROATE_DEGREE
: (-1.0f * MAX_ROATE_DEGREE);
}
// need to slow down if the distance is short
mDirection = normalizeDegree(mDirection
+ ((to - mDirection)));
mPointer.updateDirection(mDirection);// 更新指南针旋转
}
Message msg = new Message();
msg.what = 1;
msg.arg1 = progress;
mHandler.sendMessage(msg);
mRoundProgressBar1.setProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
/**
* 调整方向传感器获取的值
*/
private float normalizeDegree(float degree) {
return (degree + 720) % 360;
}
}
package com.example.car_custom_circleview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* 自定义一个View继承ImageView,增加一个通用的旋转图片资源的方法
*/
@SuppressLint("AppCompatCustomView")
public class CompassView extends ImageView {
private float mDirection;// 方向旋转浮点数
private Drawable compass;// 图片资源
//三个构造器
public CompassView(Context context) {
super(context);
// 默认不旋转
mDirection = 0.0f;
compass = null;
}
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
mDirection = 0.0f;
compass = null;
}
public CompassView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDirection = 0.0f;
compass = null;
}
@Override
protected void onDraw(Canvas canvas) {
if (compass == null) {
compass = getDrawable();// 获取当前view的图片资源
compass.setBounds(0, 0, getWidth(), getHeight());// 图片资源在view的位置,此处相当于充满view
}
canvas.save();
canvas.rotate(mDirection, getWidth() / 2, getHeight() / 2);// 绕图片中心点旋转,
compass.draw(canvas);// 把旋转后的图片画在view上,即保持旋转后的样子
canvas.restore();// 保存一下
}
/**
* 自定义更新方向的方法
*
* @param direction 传入的方向
*/
public void updateDirection(float direction) {
mDirection = direction;
postInvalidate();// 重新刷新一下,更新方向
}
}