
I'm still trying to solve a problem that arose a couple of days ago, and still have not found a solution. However, I get there step by step. Now I came across another roadblock.
I am trying to force Bitmap.getpixel(int x, int y)to return Colorwhat the user has touched using OnTouchListener. Pie is a resource VectorDrawable. vectordrawable.xmlI don’t need to do anything with pixel data yet, I just need to test it. So I did TextView, which spit out Color, which was touched.
public class MainActivity extends AppCompatActivity {
ImageView imageView;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
textView = (TextView) findViewById(R.id.textView);
imageView.setOnTouchListener(imageViewOnTouchListener);
}
View.OnTouchListener imageViewOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Drawable drawable = ((ImageView)view).getDrawable();
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
int x = (int)event.getX();
int y = (int)event.getY();
int pixel = bitmap.getPixel(x,y);
textView.setText("touched color: " + "#" + Integer.toHexString(pixel));
return true;
}
};
}
But my application encounters a fatal error, as soon as I touch ImageView, it displays the message "Sorry ..." and quits. In the stack trace, I found this.
java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
at com.skwear.colorthesaurus.MainActivity$1.onTouch(MainActivity.java:38)
and line 38 is this one,
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
. ? VectorDrawable. , Color? , BitmapFactory Drawable. API VectorDrawable, API 21?