CustomView類:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
public class CustomView extends View {
//定義幾個必要的變量
private float currentValue = 0; // 用於紀錄當前的位置,取值範圍[0,1]映射Path的整個長度
private float[] pos; // 當前點的實際位置
private float[] tan; // 當前點的tangent值,用於計算圖片所需旋轉的角度
private Bitmap mBitmap; // 箭頭圖片
private Matrix mMatrix; // 矩陣,用於對圖片進行一些操作
private int color;
private Paint paint;
private Paint mPaint;
public CustomView(Context context) {
super(context);
init(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.CustomView_viewColor:
// 默認顏色設置,黑色
color = typedArray.getColor(attr, Color.BLACK);
break;
}
}
typedArray.recycle();
mPaint = new Paint();
mPaint.setColor(color);
}
// 初始化這些變量(在構造函數中調用這個方法):
private void init(Context context) {
pos = new float[2];
tan = new float[2];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6; // 縮放圖片
mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.jiantou2, options);
mMatrix = new Matrix();
}
//具體繪製:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(color);
canvas.translate(200, 200); // 平移座標系
Path path = new Path(); // 創建 Path
path.addCircle(200, 200, 200, Path.Direction.CW); // 添加一個圓形
PathMeasure measure = new PathMeasure(path, false); // 創建 PathMeasure
currentValue += 0.005; // 計算當前的位置在總長度上的比例[0,1]
if (currentValue >= 1) {
currentValue = 0;
}
//這個方法是用於得到路徑上某一長度的位置以及該位置的正切值:
measure.getPosTan(measure.getLength() * currentValue, pos, tan); // 獲取當前位置的座標以及趨勢
mMatrix.reset(); // 重置Matrix
float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI); // 計算圖片旋轉角度
mMatrix.postRotate(degrees, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2); // 旋轉圖片
mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2, pos[1] - mBitmap.getHeight() / 2); // 將圖片繪製中心調整到與當前點重合
canvas.drawPath(path, paint); // 繪製 Path
canvas.drawBitmap(mBitmap, mMatrix, paint); // 繪製箭頭
invalidate(); // 重繪頁面
}
//改變顏色
public void setColor(int red) {
color = red;
}
}
MainActivity類:
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button change;
private Button up;
private Button down;
private CustomView myview;
private boolean isFly = true;
private int index = 10;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0: {
myview.invalidate();
}
break;
case 1: {
myview.invalidate();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//剪頭開始移動
startFly(isFly);
}
private void startFly(final boolean isFly) {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (isFly) {
if (index > 0) {
Thread.sleep(index);
handler.sendEmptyMessage(0);
} else {
Thread.sleep(10);
handler.sendEmptyMessage(1);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void initView() {
change = (Button) findViewById(R.id.change);
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
myview = (CustomView) findViewById(R.id.myview);
change.setOnClickListener(this);
up.setOnClickListener(this);
down.setOnClickListener(this);
myview.setColor(Color.BLACK);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.change:
myview.setColor(Color.RED);
break;
case R.id.up:
index = index - 10;
if (index <= 0) {
Toast.makeText(MainActivity.this, "閃電般:" + index, Toast.LENGTH_SHORT).show();
}
break;
case R.id.down:
index = index + 10;
break;
}
}
}
佈局頁面:
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>
<Button
android:id="@+id/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="改變顏色"
/>
<Button
android:id="@+id/up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加速"
/>
<Button
android:id="@+id/down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="減速"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />
<com.bwei.cpm.CustomView
android:id="@+id/myview"
android:layout_width="400dp"
android:layout_height="400dp"
app:viewColor="@android:color/black"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp" />
</LinearLayout>