Android ImageView onClick

时间:2015-08-06 19:46:18

标签: android image button onclick imageview

我是Android开发的新手。 我在阵列中有5个图像,我想在点击时更改图片,所以这是我做的,但图片不会改变。

 int[] images = {R.drawable.pic_1,
        R.drawable.pic_2,
        R.drawable.pic_3,
        R.drawable.pic_4,
        R.drawable.pic_5};
     int plus = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main2);

        ImageView img = (ImageView) findViewById(R.id.imageView);
        Bitmap bImage ;

        Button b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                plus++;
            }
        });

        Button b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                plus--;
            }
        });


            bImage =  BitmapFactory.decodeResource(this.getResources(),images[plus]);
            img.setImageBitmap(bImage);



    }

int[] images = {R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5}; int plus = 0 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main2); ImageView img = (ImageView) findViewById(R.id.imageView); Bitmap bImage ; Button b1 = (Button) findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { plus++; } }); Button b2 = (Button) findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { plus--; } }); bImage = BitmapFactory.decodeResource(this.getResources(),images[plus]); img.setImageBitmap(bImage); }

任何人都有这样的想法吗? 提前谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个:

 int[] images = {R.drawable.pic_1,
        R.drawable.pic_2,
        R.drawable.pic_3,
        R.drawable.pic_4,
        R.drawable.pic_5};
     int plus = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main2);

        ImageView img = (ImageView) findViewById(R.id.imageView);
        Bitmap bImage ;

        Button b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if(plus<4){
                plus++;
                img.setBackgroundResource(images[plus]);
              }
            }
        });

        Button b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(plus>0){
                plus--;
                img.setBackgroundResource(images[plus]);
                }
            }
        });


            bImage =  BitmapFactory.decodeResource(this.getResources(),images[plus]);
            img.setImageBitmap(bImage);



    }

答案 1 :(得分:0)

我认为你应该精通Android Activity Lifecycle。从android开发开始,基本的理解对你很有帮助。

在创建活动时,只调用一次OnCreate方法。因此,在onclicklisteners中,您还应该放置用于更改图像位图的代码。所以你的onclicklistener应该是这样的:

plus++;
bImage = BitmapFactory.decodeResource(this.getResources(),images[plus]);
img.setImageBitmap(bImage); 

这只是上述问题的解决方案,但我强烈建议您通过基本的Android概念来开始您的应用开发,而不会遇到琐碎的问题。

祝你好运。!

答案 2 :(得分:0)

设置全局变量

private int imageclick=0; 

并使用以下逻辑

    ImageView img = (ImageView) findViewById(R.id.imageView);
    Bitmap bImage ;

    Button b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    ImageView img = (ImageView) findViewById(R.id.imageView);
    Bitmap bImage ;

    Button b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

if(imageclick==0)
{
img.setImageResource(R.drawable.pic_1);
imageclick=1;
}
else if(imageclick==1)
{
img.setImageResource(R.drawable.pic_2);
imageclick=2;
}
else if(imageclick==2)
{
img.setImageResource(R.drawable.pic_3);
imageclick=3;
}
else if(imageclick==3)
{
img.setImageResource(R.drawable.pic_4);
imageclick=4;
}
else if(imageclick==4)
{
img.setImageResource(R.drawable.pic_5);
imageclick=0;
}
        }
    });

    Button b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

if(imageclick==0)
{
img.setImageResource(R.drawable.pic_4);
imageclick=4;
}
else if(imageclick==1)
{
img.setImageResource(R.drawable.pic_3);
imageclick=0;
}
else if(imageclick==2)
{
img.setImageResource(R.drawable.pic_2);
imageclick=1;
}
else if(imageclick==3)
{
img.setImageResource(R.drawable.pic_1);
imageclick=2;
}
else if(imageclick==4)
{
img.setImageResource(R.drawable.pic_5);
imageclick=3;
}
      }
        }
    });


        bImage =  BitmapFactory.decodeResource(this.getResources(),images[plus]);
        img.setImageBitmap(bImage);



}

这个逻辑可能适合你