public class TouchView extends ImageView { Bitmap bitmap; double bmWidth, bmHeight; public TouchView(Context context) { super(context); // TODO Auto-generated constructor stub init(); } public TouchView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } public TouchView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(); } private void init(){ bitmap = ((BitmapDrawable)getBackground()).getBitmap(); bmWidth = (double)bitmap.getWidth(); bmHeight = (double)bitmap.getHeight(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch(event.getAction()){ case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: float x = event.getX(); float y = event.getY(); int color = getColor(x, y); ((AndroidDetechTouchActivity)getContext()).updateMsg(" Touched@ " + x + " : " + y, color); break; case MotionEvent.ACTION_UP: ((AndroidDetechTouchActivity)getContext()).updateMsg("", 0); break; } return true; } private int getColor(float x, float y){ if ( x < 0 || y < 0 || x > (float)getWidth() || y > (float)getHeight()){ return 0; //Invalid, return 0 }else{ //Convert touched x, y on View to on Bitmap int xBm = (int)(x * (bmWidth / (double)getWidth())); int yBm = (int)(y * (bmHeight / (double)getHeight())); return bitmap.getPixel(xBm, yBm); } } }
Change the updateMsg () method of the main activity, AndroidDetechTouchActivity.java to enable color and update the TextView TextColor.
public class AndroidDetechTouchActivity extends Activity { TextView msg; TouchView touchView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); msg = (TextView)findViewById(R.id.msg); touchView = (TouchView)findViewById(R.id.touchview); } public void updateMsg(String tMsg, int color){ msg.setTextColor(color); msg.setText(tMsg); } }
source share