0

Android获取点击的图片的颜色

获取图片中点击的颜色

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView touchedXY, invertedXY, imgSize, colorRGB;
ImageView imgSource1, imgSource2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touchedXY = (TextView)findViewById(R.id.xy);
invertedXY = (TextView)findViewById(R.id.invertedxy);
imgSize = (TextView)findViewById(R.id.size);
colorRGB = (TextView)findViewById(R.id.colorrgb);
imgSource1 = (ImageView)findViewById(R.id.source1);
imgSource2 = (ImageView)findViewById(R.id.source2);
imgSource1.setOnTouchListener(imgSourceOnTouchListener);
imgSource2.setOnTouchListener(imgSourceOnTouchListener);
}

OnTouchListener imgSourceOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] {eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView)view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]);
int y = Integer.valueOf((int)eventXY[1]);
touchedXY.setText( “touched position: ” + String.valueOf(eventX) + ” / ” + String.valueOf(eventY)); invertedXY.setText(“touched position: ” + String.valueOf(x) + ” / ” + String.valueOf(y));
Drawable imgDrawable = ((ImageView)view).getDrawable();
Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();
imgSize.setText(“drawable size: “+ String.valueOf(bitmap.getWidth()) + ” / “+ String.valueOf(bitmap.getHeight()));
//Limit x, y range within bitmap
if(x < 0){x = 0; }
else if(x > bitmap.getWidth()-1) {
x = bitmap.getWidth()-1;
}
if(y < 0){
y = 0;
}else if(y > bitmap.getHeight()-1) {
y = bitmap.getHeight()-1;
}
int touchedRGB = bitmap.getPixel(x, y);
colorRGB.setText(“touched color: ” + “#” + Integer.toHexString(touchedRGB));
colorRGB.setTextColor(touchedRGB);
return true;

}
};
}

 

布局

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”     xmlns:tools=”http://schemas.android.com/tools”     android:layout_width=”match_parent”     android:layout_height=”match_parent”      android:orientation=”vertical”>      <TextView         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:text=”@string/hello_world”         tools:context=”.MainActivity” />     <TextView          android:id=”@+id/xy”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:text=”touched position: “/>     <TextView          android:id=”@+id/invertedxy”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:text=”inverted touched position: “/>     <TextView          android:id=”@+id/size”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:text=”drawable size: “/>     <TextView          android:id=”@+id/colorrgb”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:text=”touched color: “/>     <ImageView         android:id=”@+id/source1″         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”         android:src=”@drawable/ic_launcher”/>     <ImageView         android:id=”@+id/source2″         android:layout_width=”match_parent”         android:layout_height=”match_parent”         android:src=”@drawable/ic_launcher”/>  </LinearLayout>

天边的星星