public class MaterialsView extends CardView {
private Paint paint ;
private Paint paint2 ;
private float redius=0;
private Bitmap bitmap;
private Bitmap bitmap2;
private boolean ishe;
private Matrix mMatrix;
public MaterialsView(Context context) {
this(context, null);
}
public MaterialsView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MaterialsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
bitmap2= bitmap = getBitmap(context, R.mipmap.ceshi);
paint=new Paint();
paint2=new Paint();
paint.setAntiAlias(true);
paint2.setAntiAlias(true);
ishe=true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mMatrix =new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF= new RectF();
rectF.left=0;
rectF.top=0;
rectF.right=getWidth();
rectF.bottom=getHeight();
canvas.drawBitmap(bitmap2,mMatrix,paint2);
canvas.drawCircle(getWidth()/2,getHeight()/2,redius,paint);
}
private static Bitmap getBitmap(Context context,int vectorDrawableId) {
Bitmap bitmap=null;
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
Drawable vectorDrawable = context.getDrawable(vectorDrawableId);
bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
}else {
bitmap = BitmapFactory.decodeResource(context.getResources(), vectorDrawableId);
}
return bitmap;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
//获取当前坐标
switch (action) {
case MotionEvent.ACTION_DOWN:
ObjectAnimator.ofFloat(this, "translationZ", dip2px(getContext(),8)).setDuration(390).start();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
ObjectAnimator.ofFloat(this, "translationZ", dip2px(getContext(),2)).setDuration(390).start();
handler.sendEmptyMessage(1);
break;
}
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
getParent().requestDisallowInterceptTouchEvent(true);//请求父布局 不要拦截,那区域内 父布局事件不在响应
return super.dispatchTouchEvent(ev);
}
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
Handler handler =new Handler(getContext().getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what){
case 1:
if(redius==0) {
bitmap2=bitmap;
if(ishe) {
Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
bitmap.eraseColor(color);//填充颜色
ishe=false;
Log.e("TGA","纯色");
}else {
bitmap = getBitmap(getContext(), R.mipmap.ceshi);
ishe=true;
Log.e("TGA","图片");
}
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
// 设置变换矩阵
bitmapShader.setLocalMatrix(mMatrix);
paint.setShader(bitmapShader);
new Thread(() -> {
for (int i = 0; i < 24; i++) {
redius+=15;
handler.sendEmptyMessage(2);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
redius = 0;
}).start();
}
break;
case 2:
invalidate();
break;
}
}
};
复制代码
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END