我在android studio中有两个活动。在Activity1中我有一个按钮或图像视图,我希望当我点击它时,将图像发送到Activity2,当我到达Activity2时,图像将在那里。在Activity1中我第二次单击该按钮时,Activity2中的图像将消失。 但我不想点击图像视图并转到第二个活动。我想在第一个活动中点击它,然后将图像发送到sec活动,而不自动转到sec活动。当我手动进入秒活动时,图像将在那里。当我返回第一个活动并再次单击图像视图时,sec活动中的图像将消失。我怎样才能做到这一点?
public class MainActivity extends AppCompatActivity {
byte[] bytesImage;
public static final String MyPREFERENCES = "MyPrefs";
//use shared preferences to save button click count
SharedPreferences sharedPreferences;
String hide_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
final ImageView imageview=(ImageView) findViewById(R.id.imageView);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sharedPreferences.getString("activity1", "").equals("1")) {
//image is already clicked once
hide_image="1";
}
else{
hide_image="0";
imageview.buildDrawingCache(); //your imageview
Bitmap bitmap = imageview.getDrawingCache();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, ba);
//save the image byte array
bytesImage=ba.toByteArray();
SharedPreferences.Editor editor = sharedPreferences.edit();
//set 1 as button is clicked
editor.putString("activity1", "1");
editor.commit();
}
}
});
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("byteArray", bytesImage);
i.putExtra("hideImage", hide_image);
startActivity(i);
}
});
}
}
答案 0 :(得分:1)
在您的第一项活动中:使用
byte[] bytesImage;
public static final String MyPREFERENCES = "MyPrefs";
//use shared preferences to save button click count
SharedPreferences sharedPreferences;
String hide_image;
onCreate()中的
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
然后设置imageview点击监听器
imageview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (sharedPreferences.getString("activity1", "").equals("1")) {
//image is already clicked once
hide_image="1";
}
else{
hide_image="0";
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, ba);
//save the image byte array
bytesImage=ba.toByteArray()
SharedPreferences.Editor editor = sharedPreferences.edit();
//set 1 as button is clicked
editor.putString("activity1", "1");
editor.commit();
}
}
});
manualButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(this, NextActivity.class);
i.putExtra("byteArray", bytesImage);
i.putExtra("hideImage", hide_image);
startActivity(i);
}
});
在你的第二个活动中
String hide=getIntent().getStringExtra("hideImage");
byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if (hide.equals("0") {
//show image only in first click
ImageView previewThumbnail = new ImageView(this);
previewThumbnail.setImageBitmap(bmp);//set bitmap to imageview
}