Skip to content

Instantly share code, notes, and snippets.

@happycodinggirl
Last active August 29, 2015 14:18
Show Gist options
  • Save happycodinggirl/46a3fd6c9dbe9918aa96 to your computer and use it in GitHub Desktop.
Save happycodinggirl/46a3fd6c9dbe9918aa96 to your computer and use it in GitHub Desktop.
比较有意思的ImageView
package com.example.huangxingli.androidshaderabout;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.text.LoginFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* Created by huangxingli on 2015/4/7.
*/
public class GuaGuaImageView extends ImageView {
float dx;
float dy;
final float radius=60;
boolean shouldGua;
Paint paint;
public GuaGuaImageView(Context context) {
super(context);
}
public GuaGuaImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GuaGuaImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action=event.getAction();
shouldGua=action==MotionEvent.ACTION_DOWN||action==MotionEvent.ACTION_MOVE;
dx=event.getX();
dy=event.getY();
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (paint==null){
paint=new Paint();
Bitmap bitmap= Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
Log.v("TAG","====GETWIDTH IS "+getWidth());
Canvas originCanvas=new Canvas(bitmap);
super.onDraw(originCanvas);
BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(bitmapShader);
}
canvas.drawColor(Color.GRAY);
if (shouldGua) {
//dx dy circle的圆心的x y轴坐标
//radius半径长度
canvas.drawCircle(dx, dy, radius, paint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment