按下按钮的图像,图像放大

时间:2015-07-21 14:18:50

标签: android

新的Android开发并遇到了障碍,我正在制作一个会有很多照片的应用程序,我想要实现的是在我按下主要活动中的按钮带我去另一个活动后,我会就像一个充满按钮的页面,这个新活动一旦按下每个按钮就会显示一个图像并有一些我想添加的文字,我想这样做而不必为每张照片创建一个新的活动所以我希望有人我可以帮助我!把它想象成一个sounbank应用程序,当按下每行按钮时播放声音!在我的情况下,它将显示每个按钮的图像,这可以仅使用主要活动和额外的活动完成,或者我最终将在项目浏览器中为每张图片打开一个活动。 你的帮助很大,谢谢你。

1 个答案:

答案 0 :(得分:0)

Ok lets assume you are using drawables from your resource folder. In that way you deal with integer ids that represent the images. These id can be transmitted per Intent to a second activity, that retrieves the intent, parses the id and loads / shows the image.

In your Activity A, where you have all your buttons, I assume further that you've added OnClicklisteners to the button like:

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //something
            }
        });

In your click listener you replace //something with:

    Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
    i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, R.drawable.oneOfYourImages);
    startActivity(i);

The first argument is the key to retrieve the id later and the second is the id. The R.drawable.oneOfYourImages depends on which button got clicked and shoud be different for every button.

You can do this for every button, or you create a method that returns an OnClickListener with the id as parameter.

private View.OnClickListener showImageFor(final int resIdOfImage) {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
            i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, resIdOfImage);
            startActivity(i);
        }
    };
}

Now, in Your ActivityToShowImage, you override the onCreate like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        int imageId = 0;
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            imageId = extras.getInt(IMAGE_KEY_SOME_STRING, 0);
        }
        if(imageId == 0){
            //some log message or exception and don't forget to finish in onResume()
        }else{
            loadImage(imageId);
        }
}

And in your loadImage method you load the image via the id into an ImageView. I hope that is the answer to your question and sorry for the late response.