我在活动中有一个 onClick() 方法,并且该 onClick() 方法更改了 ImageView 的照片。我想使 ImageView 无效并在 onClick() 之后重新绘制它,但是 imageView.invalidate() 或 imageView.postInvalidate() 或 view.invalidate() 或 view.postInvalidate() 都不会导致 ImageView 刷新。相反,它会在下次单击时刷新。这是方法:
public void onImageViewProfileClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
// Use this to restrict items to those on the SD card:
photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(photoPickerIntent, REQUEST_CODE);
if (image != null) {
imageViewProfile.setImageBitmap(image);
view.postInvalidate();
}
}
有谁知道解决方案?
这是完整的方法:
package com.example.news_app.android.ui;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.example.news_app.android.DataManager;
import com.example.news_app.R;
import com.example.news_app.android.Settings;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class SettingsActivity extends Activity {
private Settings appSettings = new Settings(this);
private EditText editTextName;
private EditText editTextAbout;
private EditText editTextEmail;
private EditText editTextPhone;
private ImageView imageViewProfile;
private Bitmap image = null;
public static final int REQUEST_CODE = 1;
// The image will be scaled to 32x32
public static final int IMAGE_SIZE = 32;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAbout = (EditText) findViewById(R.id.editTextAbout);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPhone = (EditText) findViewById(R.id.editTextPhone);
imageViewProfile = (ImageView) findViewById(R.id.imageViewProfile);
editTextName.setText(appSettings.getName());
editTextAbout.setText(appSettings.getAbout());
editTextEmail.setText(appSettings.getEmail());
editTextPhone.setText(appSettings.getPhone());
Bitmap profile = appSettings.getProfile();
if (profile != null) {
imageViewProfile.setImageBitmap(profile);
}
if (appSettings.getName().equals("")) {
Toast.makeText(this,
"You have to enter a name before you can post or comment",
Toast.LENGTH_LONG).show();
}
}
public void onImageViewProfileClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
// Use this to restrict items to those on the SD card:
photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(photoPickerIntent, REQUEST_CODE);
if (image != null) {
imageViewProfile.setImageBitmap(image);
view.postInvalidate();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e1) {
Log.d("onActivityResult", "FileNotFoundException");
e1.printStackTrace();
}
Bitmap unscaledImage = BitmapFactory.decodeStream(inputStream,
null, null);
image = ThumbnailUtils.extractThumbnail(unscaledImage, IMAGE_SIZE,
IMAGE_SIZE);
Log.d("Unscaled image byte count",
"Bytes: " + unscaledImage.getByteCount());
Log.d("Scaled image byte count", "Bytes: " + image.getByteCount());
}
}
public void onButtonSaveClick(View view) {
String editTextNameInput = editTextName.getText().toString();
String editTextAboutInput = editTextAbout.getText().toString();
String editTextEmailInput = editTextEmail.getText().toString();
String editTextPhoneInput = editTextPhone.getText().toString();
Bitmap imageViewProfileInput = ((BitmapDrawable) imageViewProfile
.getDrawable()).getBitmap();
if (editTextNameInput != null) {
appSettings.setName(editTextNameInput);
}
if (editTextAboutInput != null) {
appSettings.setAbout(editTextAboutInput);
}
if (editTextEmailInput != null) {
appSettings.setEmail(editTextEmailInput);
}
if (editTextPhoneInput != null) {
appSettings.setPhone(editTextPhoneInput);
}
if (imageViewProfileInput != null) {
appSettings.setProfile(imageViewProfileInput);
}
DataManager dataManager = new DataManager(this);
dataManager.addUser(appSettings.getUserId(), appSettings.getName(),
appSettings.getAbout(), appSettings.getEmail(),
appSettings.getPhone());
this.finish();
}
}