单击图像视图时,其他图像也会缩小

时间:2014-09-26 10:26:00

标签: android bitmap imageview android-animation

我正在开发一个捕获图像然后比较图像的应用程序。在主屏幕或第一个屏幕上,我有一个图像视图,表示点击n比较。当我点击它时,它打开相机,捕获图片,然后在第二个活动上显示图像。在第二个活动中,我有四个图像视图和一个捕获照片的按钮。现在当我点击图像时,它应该放大,当我再次点击它应该放大它。这适用于第一张图像,但对于其他图像,当我缩小时,它也放大其他图像。请告诉我我错在哪里。我已将相机权限放在Manifest文件中,下面是第二个活动的代码(CompareActivity.java)

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

/ *     *     * @author用户     *      * /

  public class CompareActivity extends Activity {

ImageView imv_compare_pic_1, imv_compare_pic_2, imv_compare_pic_3,
        imv_compare_pic_4;
Button btn_capture_photo;
int count = 0;
boolean[] pressed = { false, false, false, false };
Animation zoomin, zoomout;

    /*
    * (non-Javadoc)
    * 
    * @see android.app.Activity#onCreate(android.os.Bundle)
    */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compare);
    imv_compare_pic_1 = (ImageView) findViewById(R.id.imv_compare_pic_1);
    Bitmap bitmap = getIntent().getExtras().getParcelable("image");
    imv_compare_pic_1.setImageBitmap(bitmap);
    /*
     * this variable is to check the number of images that have been placed
     * in activity. Since one image has been set, count is incremented to 1
     */
    count = 1;

    // zoom in and zoom out
    zoomin = AnimationUtils.loadAnimation(this, R.anim.zoomin);
    zoomin.setFillAfter(true);
    zoomout = AnimationUtils.loadAnimation(this, R.anim.zoomout);

    imv_compare_pic_1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /*
             * pressed[0] is false because first image hasn't been clicked
             * so it will zoom in
             */
            if (!pressed[0]) {
                imv_compare_pic_1.startAnimation(zoomin);
                pressed[0] = !pressed[0];

            } else {
                // Now pressed[0] is true so it will start zoom out
                // animation
                imv_compare_pic_1.startAnimation(zoomout);
                pressed[0] = !pressed[0];

            }
        }
    });

    btn_capture_photo = (Button) findViewById(R.id.btn_capture);
    btn_capture_photo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            open();
        }
    });
}

public void open() {
    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
    if (count == 1) {
        imv_compare_pic_2 = (ImageView) findViewById(R.id.imv_compare_pic_2);
        imv_compare_pic_2.setImageBitmap(bp);
        count = 2;
        // set zoom in and zoom out on the event of onclick
        // imv_compare_pic_2.setAnimation(zoomin);
        // imv_compare_pic_2.setAnimation(zoomout);
        imv_compare_pic_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v2) {
                // TODO Auto-generated method stub

                if (!pressed[1]) {
                    imv_compare_pic_2.startAnimation(zoomin);
                    pressed[1] = !pressed[1];

                } else {
                    imv_compare_pic_2.startAnimation(zoomout);
                    pressed[1] = !pressed[1];

                }
            }
        });

    }

    else if (count == 2) {
        imv_compare_pic_3 = (ImageView) findViewById(R.id.imv_compare_pic_3);
        imv_compare_pic_3.setImageBitmap(bp);
        count = 3;

        // set zoom in and zoom out on the event of onclick
        // imv_compare_pic_3.setAnimation(zoomin);
        // imv_compare_pic_3.setAnimation(zoomout);

        imv_compare_pic_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v3) {
                // TODO Auto-generated method stub

                if (!pressed[2]) {
                    imv_compare_pic_3.startAnimation(zoomin);
                    pressed[2] = !pressed[2];

                } else {
                    imv_compare_pic_3.startAnimation(zoomout);
                    pressed[2] = !pressed[2];
                }
            }
        });

    } else if (count == 3) {
        imv_compare_pic_4 = (ImageView) findViewById(R.id.imv_compare_pic_4);
        imv_compare_pic_4.setImageBitmap(bp);
        count = 4;
        // btn_capture_photo.setVisibility(View.INVISIBLE);

        // set zoom in and zoom out on the event of onclick
        // imv_compare_pic_4.setAnimation(zoomin);
        // imv_compare_pic_4.setAnimation(zoomout);
        imv_compare_pic_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v4) {
                // TODO Auto-generated method stub

                if (!pressed[3]) {
                    imv_compare_pic_4.startAnimation(zoomin);
                    pressed[3] = !pressed[3];
                } else {
                    imv_compare_pic_4.startAnimation(zoomout);
                    pressed[3] = !pressed[3];
                }
            }
        });
    }

}

}

1 个答案:

答案 0 :(得分:0)

您正在开始动画后递增计数。

另外,你只为pressed状态维护一个布尔值。

我建议你使用一个数组作为布尔值,因为你需要pressed状态用于特定的imageview。

类似的东西:

//in onCreate 
boolean[] pressed = {false, false, false, false};

//an example for one imageview
...
else if (count == 2) {
    imv_compare_pic_3 = (ImageView) findViewById(R.id.imv_compare_pic_3);
    imv_compare_pic_3.setImageBitmap(bp);
    count = 3;

    // set zoom in and zoom out on the event of onclick
    //imv_compare_pic_3.setAnimation(zoomin);
    //imv_compare_pic_3.setAnimation(zoomout);

    imv_compare_pic_3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v3) {
            // TODO Auto-generated method stub

            if (!pressed[2]) {
                imv_compare_pic_3.startAnimation(zoomin);
                pressed[2] = !pressed[2];
            } else {
                imv_compare_pic_3.startAnimation(zoomout);
                pressed[2] = !pressed[2];
            }
        }
    });
}

请重新检查您的计数。从1到4,我建议将其从0更改为3.因此,使用布尔数组索引很容易。

希望这能解决您的问题。