layoutに画像貼り付け

124 views
Skip to first unread message

unread,
Oct 26, 2011, 11:02:37 PM10/26/11
to android-g...@googlegroups.com
はじめましてプログラミング経験5ヶ月の林と申します。

経験はかなり浅いですが
ギャラリーから選んだ画像を編集できるお絵かきアプリのようなものを作ろうとおもっています。

mainとなるクラスからintentを使ってsubクラスを起動
subクラスでギャラリーを呼びだす
選択した画像(a)をmainクラスのonActivityReultに渡す
受け取った画像UriをmainクラスのImageLayoutで表示。



このような動きにしたいのですが画像UriをImageLayoutで表示させるやり方が
わからなくて困っています。

ImageLayoutには別の画像(b)をすでに表示させていますが、
同じ場所に画像(a)を表示させたいです。
ImageLayoutに画像(a)を貼り付けたら、画像(b)はもう必要ありません。

Bitmap bitmap = BitmapFactory.decodeResource(
getResources(),R.drawable.aaaa);



ImageView imageview = new ImageView(this);
imageview.setImageBitmap(bitmap);
imageview.setLayoutParams(new LinearLayout.LayoutParams(FP,WC));
Imagelayout.addView(imageview);




protected void onActivityResult(int requestCode, 
int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_PHOTO){
if(resultCode == RESULT_OK){
Intent i = data;
Bundle extras = i.getExtras();
Uri photoUri = (Uri)extras.get("INPUT");
ContentResolver cont_reslv = getContentResolver();
InputStream in = null;
View m = this.getLayoutInflater().inflate(R.layout.main,null);
mImageView=(ImageView)m.findViewById(R.id.ImageView02);

try{
in = getContentResolver().openInputStream(photoUri);
mBitmap = BitmapFactory.decodeStream(in);
mBitmap.recycle();
mBitmap = MediaStore.Images.Media.getBitmap(cont_reslv, photoUri);
mImageView.setImageBitmap(mBitmap);
mImageView.invalidate();
}catch(IOException e){
e.printStackTrace();
}
}
}
}

mImageViewwをImagelayoutにで表示させるやり方、ご教授願います。

エプシロン

unread,
Oct 27, 2011, 4:21:04 AM10/27/11
to 日本Androidの会
はじめまして。えなかと申します。

> ImageLayoutには別の画像(b)をすでに表示させていますが、
> 同じ場所に画像(a)を表示させたいです。
> ImageLayoutに画像(a)を貼り付けたら、画像(b)はもう必要ありません。

ImageLayoutというのは、
ImageViewが配置されているLinearLayoutか何かでしょうか?

コードを見させていただきましたが、
同じ場所に表示させたいということなのであれば、
新しくImageViewを生成しなくても、
取得したBitmapを改めてsetImageBitmapすれば上書きできます。

下記のような方法はどうでしょうか?

参考になれば幸いです。

public class ImageTestActivity extends Activity {

LinearLayout Imagelayout;
Bitmap mBitmap;
ImageView mImageView;
private Button button;

int REQUEST_PHOTO = 1;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Imagelayout = (LinearLayout) findViewById(R.id.imagelayout);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);

button = new Button(this);
button.setText("ギャラリー呼び出し");
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_PHOTO);
}
});
Imagelayout.addView(button);

mImageView = new ImageView(this);
mImageView.setImageBitmap(bitmap);
mImageView.setLayoutParams(new LinearLayout.LayoutParams(FP, WC));
Imagelayout.addView(mImageView);

}

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

if (requestCode == REQUEST_PHOTO) {
if (resultCode == RESULT_OK) {

Uri photoUri = (Uri) data.getData();

InputStream in = null;

try {
in = getContentResolver().openInputStream(photoUri);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap image = BitmapFactory
.decodeStream(in, null, options);
in.close();

mImageView.setImageBitmap(image);

} catch (FileNotFoundException e) {

} catch (IOException e) {

unread,
Oct 28, 2011, 2:01:06 AM10/28/11
to 日本Androidの会
林です。大変参考になりました。
素人質問にもかかわらず、ご丁寧にソースまで載せて頂きありがとうございました。
本当に感謝です><
Reply all
Reply to author
Forward
0 new messages