androidで画像を表示する方法

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

androidで画像を表示する方法

#1

投稿記事 by shiro4ao » 11年前

フォトビューワーのように画像を表示してボタンで次の画像を表示していくアプリを作りたいです
ただ、普通の画像ではなく2つの画像を合成したファイルを表示していきます

androidで画像を合成して表示したのですが、
表示されるはずのボタンやテキストビューが表示されませんでした

setContentView(image, new LayoutParams(WC, WC));で合成した画像を置いているせいで
setContentView(R.layout.main);で置いたものが隠れているのかなとおもうのですが
ボタンやテキストビューを表示したまま画像を表示、更新していく方法はありますでしょうか?

コード:

//MainActivity.java
package com.example.bitmapcomposition01;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView textView;
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 
	  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
              
        Resources r = getResources();
        Bitmap bmpRed = BitmapFactory.decodeResource(r, R.drawable.red);

        //読み込んだbitmapはimmutableなのでコピーしておく
        bmpRed=bmpRed.copy(Bitmap.Config.ARGB_8888, true);
        
        //合成したいほうを読み込む
        Bitmap bmpBlack = BitmapFactory.decodeResource(r, R.drawable.black);
        
        //ベースになるbitmapを指定してCanvasを生成
        Canvas canvas=new Canvas(bmpRed);
        
        //canvasに対して描画
        canvas.drawBitmap(bmpBlack, 100,200,null);
        
        //合成結果を出力
        ImageView image =new ImageView(this);
        //合成したびっとマップをせっと
        image.setImageBitmap(bmpRed);
        
        setContentView(image, new LayoutParams(WC, WC));
        
        textView = (TextView)findViewById(R.id.textView1);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
     
        textView.setText( "X:" + e.getX() + "  Y:" + e.getY() );
     
        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.d("Sample", "ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.d("Sample", "ACTION_UP");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("Sample", "ACTION_MOVE");
            break;
        }
        return true;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

コード:

//main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="62dp"
        android:text="TextView" />

</RelativeLayout>

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: androidで画像を表示する方法

#2

投稿記事 by ISLe » 11年前

layout.mainにImageViewを配置して参照すれば良いのでは。

アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

Re: androidで画像を表示する方法

#3

投稿記事 by shiro4ao » 11年前

イメージビューimageView1をmain.xmlに追加して
imageView1 = (ImageView) findViewById(R.id.imageView1);でイメージビューを探して
imageView1.setImageBitmap(bmpRed);で合成したビットマップをセットしたのですが
実行してみるとボタンやテキストビューは表示されたのにビットマップが表示されませんでした。
なにか必要な処理があるのでしょうか・・・・。

/*追記*/
エミュレータを新しく作ったら下記のコードでビットマップが表示されました
お騒がせしてすみません。
ありがとうございました。

コード:

	private TextView textView;
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 
	 ImageView imageView1 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          
        LinearLayout linearLayout = new LinearLayout(this);
        
        Resources r = getResources();
        Bitmap bmpRed = BitmapFactory.decodeResource(r, R.drawable.red);

        //読み込んだbitmapはimmutableなのでコピーしておく
        bmpRed=bmpRed.copy(Bitmap.Config.ARGB_8888, true);
        
        //合成したいほうを読み込む
        Bitmap bmpBlack = BitmapFactory.decodeResource(r, R.drawable.black);
        
        //ベースになるbitmapを指定してCanvasを生成
        Canvas canvas=new Canvas(bmpRed);
        
        //canvasに対して描画
        canvas.drawBitmap(bmpBlack, 100,200,null);
        
        //合成結果を出力
        ImageView image =new ImageView(this);
        //合成した美ttマップをせっと
        image.setImageBitmap(bmpRed);
        
        //追加したimageviewを探し出す
        imageView1 = (ImageView) findViewById(R.id.imageView1);
     
        //合成したビットマップを張り付ける
        imageView1.setImageBitmap(bmpRed);
   //     setContentView(image, new LayoutParams(WC, WC));
        
        textView = (TextView)findViewById(R.id.textView1);
    }

コード:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="62dp"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="85dp"
        android:src="@drawable/black" />

</RelativeLayout>


閉鎖

“C言語何でも質問掲示板” へ戻る