Java – how to set a valid image in the imagebutton before clicking
I want to use the selected image to set the button with effect, then click it, and then click effect to apply the same image in ImageView, but it uses the default image instead of the selected image
imgView = (ImageView) findViewById(R.id.imgView); btnSepia = (ImageButton) findViewById(R.id.btnSepia); BitmapDrawable abmp = (BitmapDrawable) imgView.getDrawable(); bmp = abmp.getBitmap(); mProgress = (ProgressBar) findViewById(R.id.progressBar); mProgress.setVisibility(View.INVISIBLE); Intent i = new Intent( Intent.ACTION_GET_CONTENT,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); i.setType("image/"); startActivityForResult(i,RESULT_LOAD_IMAGE); final Drawable resSepia = new BitmapDrawable(getResources(),bmp); imgView.setImageDrawable(resSepia); Handler handler = new Handler(); final Runnable r = new Runnable() { @Override public void run() { if (resSepia == null) return; final ColorMatrix matrixA = new ColorMatrix(); // making image B&W matrixA.setSaturation(0); final ColorMatrix matrixB = new ColorMatrix(); // applying scales for RGB color values matrixB.setScale(1f,.95f,.82f,1.0f); matrixA.setConcat(matrixB,matrixA); final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA); resSepia.setColorFilter(filter); btnSepia.setImageDrawable(resSepia); } }; handler.postDelayed(r,1000); btnSepia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { imgView.setImageDrawable(resSepia); } });
Thank you ();
Sorry, my English is not my mother tongue
Solution
I can't get your question about what I can get from the above description. You want to change the image in action
So you can use the selector. Basically, don't put your image source directly, but use the selector in the image source
To create a selector, define the selector in the resource XML, using the following method: < selector > (specify the selector tag in XML), define an item tag below it, which will have the following attributes
<selector> <item android:state="<Give_State_HERE>" android:drawable="<GIVE_Source_of_Your_Drawable_Here"/> </selector>
Try this and specify this XML as your img: source I hope this will help