逐个动态添加图像

时间:2013-10-28 09:39:38

标签: android

活动中有图像按钮,我想逐个外部加载图像。这意味着除了第一个图像按钮外,所有图像按钮都是不可见的。点击按钮后,我应该能够加载图像,并且在按钮上加载图像后,应显示下一个按钮。我为第一个按钮实现了这一点,但是对于其余的按钮无法做到这一点。这是我的代码。

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

ImageButton a, b, c, d, e, f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = (ImageButton) findViewById(R.id.imageButton1);
    b = (ImageButton) findViewById(R.id.imageButton2);
    c = (ImageButton) findViewById(R.id.imageButton3);
    d = (ImageButton) findViewById(R.id.imageButton4);
    e = (ImageButton) findViewById(R.id.imageButton5);
    f = (ImageButton) findViewById(R.id.imageButton6);

    a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, RESULT_LOAD_IMAGE);
        }
    });



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


            ImageButton a = (ImageButton)findViewById(R.id.imageButton1);
            a.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            b.setVisibility(b.VISIBLE);


    }

}

2 个答案:

答案 0 :(得分:0)

您需要知道最后点击的按钮,以便设置图像和下一个的可见性。以下是实现这一点。

public class MainActivity extends Activity implements View.OnClickListener {

    private static int RESULT_LOAD_IMAGE = 1;

    ImageButton[] imgButtons;
    private ImageButton lastClicked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgButtons = new ImageButton[6];
        imgButtons[0] = (ImageButton) findViewById(R.id.imageButton1);
        imgButtons[1] = (ImageButton) findViewById(R.id.imageButton2);
        imgButtons[2] = (ImageButton) findViewById(R.id.imageButton3);
        imgButtons[3] = (ImageButton) findViewById(R.id.imageButton4);
        imgButtons[4] = (ImageButton) findViewById(R.id.imageButton5);
        imgButtons[5] = (ImageButton) findViewById(R.id.imageButton6);

        for(ImageButton imgButton : imgButtons){
            imgButton.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        lastClicked = (ImageButton) v; // note, only possible because we only have imageButtons with click listener
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            if(lastClicked != null){
                lastClicked.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                int numButtons = imgButtons.length;
                for(int i=0; i<numButtons; i++){
                    if(imgButtons[i] == lastClicked && ++i < imgButtons.length){
                        imgButtons[i].setVisibility(View.VISIBLE);
                        break;
                    }
                }
            }
        }

    }
}

答案 1 :(得分:0)

创建选择图像的方法。创建一个String全局变量,例如“which”。

...一样

public void getImage(String whichbutton){
   Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, RESULT_LOAD_IMAGE);
            which=whichbutton;
}

现在在每次点击按钮并调用按钮名称时调用此方法。

在onActivityResult中根据变量'value。

更改图像
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


if(which.equals("a")){
                ImageButton a = (ImageButton)findViewById(R.id.imageButton1);
                a.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                b.setVisibility(b.VISIBLE);
}else if(which.equals("b")){

 ImageButton b = (ImageButton)findViewById(R.id.imageButton2);
                b.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                c.setVisibility(b.VISIBLE);

}//and more


        }