先看一眼效果(它是可以转动的风车):
主类:
package com.example.windmill_fengche;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
public class MainActivity extends AppCompatActivity {
private myview view_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
view_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现风车的旋转效果
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_rotate);
view_id.startAnimation(animation);
}
});
}
private void initView() {
view_id = (myview) findViewById(R.id.view_id);
}
}
自定义myview:
package com.example.windmill_fengche;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class myview extends View {
public myview(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public myview(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画笔
Paint paint=new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
//画风车
paint.setColor(Color.RED);
canvas.drawLine(200,100,200,200,paint);
canvas.drawLine(300,100,200,200,paint);
canvas.drawLine(200,100,300,100,paint);
paint.setColor(Color.GREEN);
canvas.drawLine(200,200,300,200,paint);
canvas.drawLine(200,200,300,300,paint);
canvas.drawLine(300,200,300,300,paint);
paint.setColor(Color.BLUE);
canvas.drawLine(200,200,100,300,paint);
canvas.drawLine(200,200,200,300,paint);
canvas.drawLine(100,300,200,300,paint);
paint.setColor(Color.YELLOW);
canvas.drawLine(100,100,100,200,paint);
canvas.drawLine(100,100,200,200,paint);
canvas.drawLine(100,200,200,200,paint);
paint.setColor(Color.BLACK);
canvas.drawCircle(200,200,5,paint);
}
}
anim_rotate:
lalalala~