1.
public class ClassFourView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Path path1 = new Path();
Path path2 = new Path();
Point point1 = new Point(200, 200);
Point point2 = new Point(600, 200);
public ClassFourView(Context context) {
super(context);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
path1.addCircle(point1.x + 200, point1.y + 200, 150, Path.Direction.CW);
path2.setFillType(Path.FillType.INVERSE_WINDING);
path2.addCircle(point2.x + 200, point2.y + 200, 150, Path.Direction.CW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.clipPath(path1);
canvas.drawBitmap(bitmap, point1.x, point1.y, paint);
canvas.restore();
canvas.save();
canvas.clipPath(path2);
canvas.drawBitmap(bitmap, point2.x, point2.y, paint);
canvas.restore();
}
}
2.
public class ClassFourView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Point point1 = new Point(200, 200);
Point point2 = new Point(600, 200);
public ClassFourView(Context context) {
super(context);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
canvas.save();
canvas.rotate(180, point1.x + bitmapWidth / 2, point1.y + bitmapHeight / 2);
canvas.drawBitmap(bitmap, point1.x, point1.y, paint);
canvas.restore();
canvas.save();
canvas.rotate(45, point2.x + bitmapWidth / 2, point2.y + bitmapHeight / 2);
canvas.drawBitmap(bitmap, point2.x, point2.y, paint);
canvas.restore();
}
}
3.
public class ClassFourView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
Camera camera = new Camera();
int degree;
ObjectAnimator animator = ObjectAnimator.ofInt(this, "degree", 0, 180);
public ClassFourView(Context context) {
super(context);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ClassFourView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
{
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maps);
animator.setDuration(2500);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animator.start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
animator.end();
}
@SuppressWarnings("unused")
public void setDegree(int degree) {
this.degree = degree;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int x = centerX - bitmapWidth / 2;
int y = centerY - bitmapHeight / 2;
// 第一遍绘制:上半部分
canvas.save();
canvas.clipRect(0, 0, getWidth(), centerY);
canvas.drawBitmap(bitmap, x, y, paint);
canvas.restore();
// 第二遍绘制:下半部分
canvas.save();
if (degree < 90) {
canvas.clipRect(0, centerY, getWidth(), getHeight());
} else {
canvas.clipRect(0, 0, getWidth(), centerY);
}
camera.save();
camera.rotateX(degree);
canvas.translate(centerX, centerY);
camera.applyToCanvas(canvas);
canvas.translate(-centerX, -centerY);
camera.restore();
canvas.drawBitmap(bitmap, x, y, paint);
canvas.restore();
}
}