简体   繁体   中英

Change image on imagebutton click android

I am showing dynamic images for page layout control for displaying the number of pages, and now I put the by default focus on first imagebutton and now when I'll click on the next image button then I want to replace the first one with simple imagebutton and the clicked one with the focusable image button, I used invalidate() method too but not yet resolved please help.

if(i==0){
    Drawable dr = getResources().getDrawable(R.drawable.bullet_2);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true));
    b1.setImageDrawable(d);
    flag = false;
}
else {
    Drawable dr = getResources().getDrawable(R.drawable.bullet_1);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    // Scale it to 50 x 50
    Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true));
    // Set your new, scaled drawable "d"
    b1.setImageDrawable(d);
}
b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Object id = b1.getTag();
        Object tag = b1.getTag();
        System.out.println("value of id:::" +tag.toString());
        Global.tagvalue = tag.toString();

        flag = true;

        if(flag == true){
            Drawable dr = getResources().getDrawable(R.drawable.bullet_2);
            Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
            // Scale it to 50 x 50
            Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true));
            // Set your new, scaled drawable "d"
            b1.setImageDrawable(d);
            b1.invalidate();
            WindowManager w = getWindowManager(); 
            Display dis = w.getDefaultDisplay(); 
            int width = dis.getWidth(); 
            int height = dis.getHeight(); 
            System.out.println("value of width of screen::" +width);

            horlist.mNextX = width*Integer.parseInt(tag.toString());
        }
        else {
            Drawable dr = getResources().getDrawable(R.drawable.bullet_1);
            Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
            // Scale it to 50 x 50
            Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 10, 10, true));
            // Set your new, scaled drawable "d"
            b1.setImageDrawable(d);
            b1.invalidate();
        }

you can set image null

imgBtn.setImageBitmap(null);

b1.setImageDrawable(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM