美文网首页Android知识Android开发Android技术知识
安卓进阶第八篇之根据手指触摸的位置获取图片的颜色值

安卓进阶第八篇之根据手指触摸的位置获取图片的颜色值

作者: 起个牛逼的昵称 | 来源:发表于2016-12-10 14:37 被阅读1248次

这两天整理了一个自己项目中用到的获取图片颜色的效果,话不多说,先看下效果:

</br>


效果图
如上,就是根据手指触摸的位置获取颜色值,色盘是一张图片,代码也很简单,如下:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/activity_main"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    >    
<FrameLayout        
android:id="@+id/frame_layout"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_centerInParent="true"/>    
<Button        
android:id="@+id/btn"        
android:layout_width="match_parent"        
android:layout_height="45dp"        
android:text="选择的颜色"        
android:layout_alignParentBottom="true"/>
</RelativeLayout>

MainActivity :

public class MainActivity extends AppCompatActivity {    
private static final String TAG = "MainActivity";    
private FrameLayout frame_layout;    
private Button btn;    
private GetPictureColorView mGetPictureColorView;    
@Override    
protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);        
initView();        
init();    
}    
private void initView() {       
frame_layout = (FrameLayout) findViewById(R.id.frame_layout);        
btn = (Button) findViewById(R.id.btn);    
}    
private void init() {        
mGetPictureColorView = new GetPictureColorView(MainActivity.this);        
frame_layout.addView(mGetPictureColorView);        
mGetPictureColorView.setOnUpdateColorListener(new 
GetPictureColorView.OnUpdateColorListener() {            
@Override            
public void changeColor(int color) {                
btn.setBackgroundColor(color);            
}        
});    
}

GetPictureColorView:自定义view获取图片颜色

public class GetPictureColorView extends View {    
private static final String TAG = "GetPictureColorView";    
private Paint mPaint;    
private Bitmap bigIcon, smallIcon;    
private int radius;    
private int size;    
private int color = Color.WHITE;//颜色,默认为白色    
public OnUpdateColorListener onUpdateColorListener;    
private Bitmap mBackgroundBitmap;    
private Paint paint;    
private float nowX, nowY;    
private ColorBean bean;    
private int nowAction;    
public interface OnUpdateColorListener {        
void changeColor(int color);    
}    
public void setOnUpdateColorListener(OnUpdateColorListener mOnUpdateColorListener) {        
this.onUpdateColorListener = mOnUpdateColorListener;    
}    
public GetPictureColorView(Context context) {        
super(context, null);        
init();        
initView();    
}    
public GetPictureColorView(Context context, AttributeSet attrs) {        
super(context, attrs);        
init();       
initView();    
}    
private void init() {        
paint = new Paint();        
mPaint = new Paint();        
mPaint.setAntiAlias(true);        
paint.setAntiAlias(true);    
}    
private void initView() {        
mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.light_group_bg);        
size = Math.min(mBackgroundBitmap.getWidth(), 
mBackgroundBitmap.getHeight());        
radius = size / 2;        
bigIcon = BitmapFactory.decodeResource(getResources(), R.drawable.positioning_big_target);        
smallIcon = BitmapFactory.decodeResource(getResources(), R.drawable.positioning_target);        
setData();    
}    
private void setData() {        
bean = new ColorBean();        
Integer col = Color.WHITE;        
bean.x = radius;        
bean.y = radius;        
bean.color = col;        
bean.type = true;        
nowX = bean.x;        
nowY = bean.y;        
color = bean.color;    
}    
public GetPictureColorView(Context context, AttributeSet attrs, int defStyleAttr) {        
super(context, attrs, defStyleAttr);    
}    
@Override    
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       
setMeasuredDimension(size, size);    
}    
@Override    protected void onDraw(Canvas canvas) {        
//画背景色盘        
canvas.drawBitmap(mBackgroundBitmap, 0, 0, mPaint);        
if (bean.type && (Math.hypot(nowX - radius, nowY - radius) < (radius) * 8 / 10)) {            
bean.x = (int) nowX;            
bean.y = (int) nowY;            
canvas.drawBitmap(bigIcon, bean.x - bigIcon.getWidth() / 2, bean.y - bigIcon.getHeight() / 2, mPaint);        
} else {            
canvas.drawBitmap(smallIcon, bean.x - smallIcon.getWidth() / 2, bean.y - smallIcon.getHeight() / 2, mPaint);        
}        
if (nowAction == MotionEvent.ACTION_UP || nowAction == MotionEvent.ACTION_CANCEL) {            
if (onUpdateColorListener != null) {         
onUpdateColorListener.changeColor(color);            
}        
}    
}    
@Override    
public boolean onTouchEvent(MotionEvent event) {        
nowX = event.getX();        
nowY = event.getY();        
//手指按下        
if (event.getAction() == MotionEvent.ACTION_DOWN) {            
if (Math.hypot(nowX - radius, nowY - radius) < (radius) * 8 / 10) {                
bean.type = true;                
int pixel = mBackgroundBitmap.getPixel((int) event.getX(), (int) event.getY());                
color = pixel;                
bean.color = pixel;            
} else {                
return true;            
}        
}        
//手指滑动或者离开        
else if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP) {            
if ((Math.hypot(nowX - radius, nowY - radius) < (radius) * 7 / 10)) {                
int pixel = mBackgroundBitmap.getPixel((int) event.getX(), (int) 
event.getY());                
color = pixel;                
bean.color = pixel;            
} else {                
return true;            
}        
} else {            
return true;        
}        
nowAction = event.getAction();        
nowX = event.getX();        
nowY = event.getY();        
invalidate();        
return true;    
}    
private class ColorBean {        
boolean type = false;//是否点击        
int color;        
int x, y;//位置    
}
}
代码就这么多,其中最主要的还是Bitmap.getPixel(int x,int y)这个方法,参数x,y就是你手指点击的位置,这个位置一定要在图片的范围之内,返回值就是图片当前触摸点的颜色值,怎么样,是不是挺简单的呢,喜欢就一下赞,谢谢啦!!!

相关文章

网友评论

    本文标题:安卓进阶第八篇之根据手指触摸的位置获取图片的颜色值

    本文链接:https://www.haomeiwen.com/subject/mohbyttx.html