• ベストアンサー

Androidのbitmap表示について

Androidのbitmap表示について res フォルダ内に drawable というフォルダをつくり、そこに 100pixel (チェック) 100pixel の画像を入れ込んでいます。 そして、bitmapを作成して c.drawBitmap( bitmap0, 0 , 0 , null ); のような感じで表示しているのですが、サイズが100(チェック)100になっていないようなのです。 (勝手に大きくなってる?) なぜでしょうか?

  • Java
  • 回答数1
  • ありがとう数3

質問者が選んだベストアンサー

  • ベストアンサー
  • anmochi
  • ベストアンサー率65% (1332/2045)
回答No.1

密度非依存ピクセルのマジックだ。この辺は密度非依存ピクセル(dp)で検索して情報を収集してもらうしかないね。 基本的にdrawableは使わずにdrawable-mdpi、drawable-hdpiなどに適切な画像を入れるほうがよいだろう。 drawable-mdpiに入れた画像は100×100の画像は200×200の画像として扱われる。この辺も全部説明しようとすると解説本の一章分くらいになるのでAndroidにおけるピクセルの扱いについて検索していただきたい。dp、sp、pxの違いが分かれば後は全部分かるだろう。

nakisan2011_12
質問者

お礼

ありがとうございました。 いろいろ調べてやっと理解出来ました。

関連するQ&A

  • Androidアプリ開発に関する質問です。

    Androidアプリ開発に関する質問です。 Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.xxx); という記述でres/drawable-hdpi/xxx.png の画像を持ってくることができると思うのですが、 データベースに保存したString型のxxxを使って動的に画像を表示する方法が分かりません。 データベースから画像名を持ってくる前に、String型の変数(仮にstrとする)に画像名を入れて、 Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.str); と記述してもstrを変数として扱ってくれません。 どなたかres/drawable/の画像を動的に持ってくる方法を教えて頂けないでしょうか。

    • ベストアンサー
    • Java
  • Android プログラム

    (ActionBarActivity が非推奨となり) AppCompatActivity を継承して Activity を作ってみたのですが、Toolbar の Logo(アイコン)が表示できません。 Toolbar の Logo(アイコン)の表示方法を ご存知の方が居ましたら ご教授下さいませm(_ _)m 開発環境は「Android Studio」、実行環境は「Galaxy S、Galaxy Note II」の2つの端末ですチェックしています アイコン・ファイルは「C:\Android\<sdk>\platforms\android-23\data\res」フォルダー内の「drawable-mdpi、drawable-hdpi」フォルダー内のファイルを使用しています。 > ActionBar oToolbar = getSupportActionBar( ); > oToolbar.setLogo(R.drawable.ic_menu_info_details); ↑両端末で表示できませんでした。 ちなみに「AndroidManifest.xml」内で「android:logo="@drawable/ic_menu_info_details"」と設定してもダメでした。

    • ベストアンサー
    • Java
  • Android用ゲームを作っています。

    現在Androidゲームを作っているのですが、現在時間を取得し、時刻を表示したり、背景画像などを変えたりしたいのですがどのように取得し、表示させればいいのでしょうか?ソースコードは ・MainActivity.java package andorid.eample.game2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.os.Bundle; import android.app.Activity; import android.widget.LinearLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout l = new LinearLayout(this); setContentView(l); l.addView(new PView(this, null, 0)); } } ・PView.java package andorid.eample.game2; import android.annotation.SuppressLint;4 import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import android.content.res.Resources; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.text.format.Time; import android.media.MediaPlayer; public class PView extends View{ Paint paint = new Paint(); Date date = new Date(); int flg = 0; int charaX =250; int charaY = 250; Resources res = this.getContext().getResources(); Bitmap bg = BitmapFactory.decodeResource(res, R.drawable.bg1); Bitmap chara = BitmapFactory.decodeResource(res, R.drawable.charan); Bitmap charaT = BitmapFactory.decodeResource(res, R.drawable.charat); Bitmap box= BitmapFactory.decodeResource(res, R.drawable.boxt); //音楽読み込み MediaPlayer se = MediaPlayer.create(getContext(),R.raw.ehehe); public PView(Context context,AttributeSet attrs,int defStyle) { super(context,attrs); // TODO 自動生成されたコンストラクター・スタブ } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas c){ c.drawBitmap(bg,0,100,paint); if(flg == 0) { c.drawBitmap(chara,charaX,charaY,paint); } if(flg == 1) { c.drawBitmap(charaT,charaX,charaY,paint); c.drawBitmap(box,50,200,paint); } invalidate(); } public boolean onTouchEvent(MotionEvent me) { //タッチされた時 if(me.getAction() == MotionEvent.ACTION_DOWN) { if(me.getAction() == MotionEvent.ACTION_DOWN) { if(flg == 0) { flg = 1; se.start(); } else{ flg =0; } } } return false; } } このような感じになっています どうかアドバイスをお願いします。

    • ベストアンサー
    • Java
  • 現在AndroidSDKにてゲームを製作して

    いるのですが、ゲーム画面は出来ているのですが、ゲームスタート画面、ゲームオーバー画面を作りたいのですがどうすればよろしいのでしょうか、スタート画面はタッチされたらゲーム画面に切り替わるようにしたいです。 ソースは メインアクティビティーは package android.game; import android.os.Bundle; import android.app.Activity; import android.widget.LinearLayout; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout l = new LinearLayout(this); setContentView(l); l.addView(new textview(this, null, 0)); l.addView(new PView(this, null, 0)); } } で、ゲーム画面のクラスは package android.game; import android.annotation.SuppressLint; import android.content.Context; import android.game.R.id; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.Random; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Paint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import java.util.Random; public class PView extends View{ Paint paint = new Paint(); int player = 0; int playerV = 20; int tamaV = 20; int x = 100; int y = 50; int tamaY = player; int tamaX = player; int line = 550; int tama2X = 0; int tama2Y = 0; int linex = 400; int cnt = 0; int score = 0; int timerT = 30; int flg = 0; int stx = 200; int sty = 200; Random rand = new Random(); Resources res = this.getContext().getResources(); Bitmap bg = BitmapFactory.decodeResource(res, R.drawable.haikei); Bitmap pl = BitmapFactory.decodeResource(res, R.drawable.player); Bitmap tama = BitmapFactory.decodeResource(res, R.drawable.tama); Bitmap tama2 = BitmapFactory.decodeResource(res, R.drawable.tama2); Bitmap lineview = BitmapFactory.decodeResource(res, R.drawable.line); Bitmap lineview2 = BitmapFactory.decodeResource(res, R.drawable.line2); public PView(Context context,AttributeSet attrs,int defStyle) { super(context,attrs); // TODO 自動生成されたコンストラクター・スタブ } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas c){ timerT--; if(timerT < 0) { timerT = 0; } player += playerV; if(player<0 || 400<player) playerV *= -1; //弾の処理 tamaY+= 0; tama2Y += 20; if(tamaY > getHeight()) { tamaY = rand.nextInt(500)+1; } tamaX = rand.nextInt(30); tama2Y += 20; if(tama2Y > getHeight()) { tama2Y = rand.nextInt(500)+1; } tama2X = rand.nextInt(100); c.drawBitmap(bg,0,0,paint); c.drawBitmap(pl,player,y, paint); c.drawBitmap(tama,player,tamaY,paint); c.drawBitmap(tama2,tama2X,tama2Y, paint); c.drawBitmap(lineview,0,line,paint); c.drawBitmap(lineview2,0,linex, paint); invalidate(); } public boolean onTouchEvent(MotionEvent me) { //タッチされた時 if(me.getAction() == MotionEvent.ACTION_DOWN) stx = 0; { if(me.getAction() == MotionEvent.ACTION_DOWN) { if(tamaY >= line && tamaY < line + 40) { tamaY = 0; score+= 10; } } if(me.getAction() == MotionEvent.ACTION_DOWN){ if(tama2Y >= linex && tama2Y < linex + 50) { tama2Y = 0; score+= 20; } } return true; } } } このようになっています。 よろしくお願いします

  • Androidプログラムについて

    Androidのプログラムについてです。 パラパラ漫画のように、多数の画像を読み込んで表示させるプログラムを教えて下さい。 サンプルを見つけましたが、画像数が極端に多い場合、for文などで出来るのでしょうか。 サンプルは下記で見つけました。 http://monoist.atmarkit.co.jp/mn/articles/1205/21/news003.html 画像の読み込みや、 画像をアニメーションのコマとして追加していく方法が記載されていますが、 画像が多い場合、for文などに変更できるのでしょうか。 (プログラム全く初心者です。) <参考> 上記URLには以下サンプルプログラムが記載されています。 // 画像の読み込み // Drawable frame1 = con.getResources().getDrawable( R.drawable.frame1 ); Drawable frame2 = con.getResources().getDrawable( R.drawable.frame2 ); Drawable frame3 = con.getResources().getDrawable( R.drawable.frame3 ); Drawable frame4 = con.getResources().getDrawable( R.drawable.frame4 ); Drawable frame5 = con.getResources().getDrawable( R.drawable.frame5 ); // 画像をアニメーションのコマとして追加していく anim.addFrame( frame1, 60 ); anim.addFrame( frame2, 60 ); anim.addFrame( frame3, 70 ); anim.addFrame( frame4, 80 ); anim.addFrame( frame5, 120 ); anim.addFrame( frame4, 80 ); anim.addFrame( frame3, 70 ); anim.addFrame( frame2, 60 ); anim.addFrame( frame1, 60 ); <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/frame1" android:duration="60" /> <item android:drawable="@drawable/frame2" android:duration="60" /> <item android:drawable="@drawable/frame3" android:duration="70" /> <item android:drawable="@drawable/frame4" android:duration="80" /> <item android:drawable="@drawable/frame5" android:duration="120" /> <item android:drawable="@drawable/frame4" android:duration="80" /> <item android:drawable="@drawable/frame3" android:duration="70" /> <item android:drawable="@drawable/frame2" android:duration="60" /> <item android:drawable="@drawable/frame1" android:duration="60" /> </animation-list> .

    • ベストアンサー
    • Java
  • AndroidのBitmapのロードについて

    はじめまして 今OPENGLESで文字の描画を行っているのですが、何度も読み込んで解放してを繰り返していると なぜかアプリが警告なしで 強制終了します。 バイト数を抑えると一応ロードは可能なのですが、これでは文字を動的に表示できなくてこまっています。 以下がロードする文字列です public void textload(String[] str,int x,int y, int size){ // Log.v("デバッグ","ロード開始"); GL10 gl = glGraphics.getGL(); int[] textureIds = new int[1]; gl.glGenTextures(1, textureIds, 0); textureId = textureIds[0]; /** * 空のビットマップを作成し、そこへ文字を書き込むことでGL上に描画を行う。 */ //ビットマップの数値を計算 Log.v("デバッグ","ビットマップロード"); bitmap = Bitmap.createBitmap(640,480, Config.ARGB_4444); canvas = new Canvas(bitmap); Paint paint = new Paint(); Log.v("デバッグ","完了"); paint.setColor(Color.WHITE); paint.setStyle(Style.FILL); paint.setTextSize(size); canvas.drawColor(0); for(int i=0;i<str.length;i++){ if(str[i] != null){ canvas.drawText(str[i], x, y + size*(i+1), paint); } } width = bitmap.getWidth(); height = bitmap.getHeight(); gl.glEnable(GL10.GL_TEXTURE_2D); Log.v("デバッグ","ビットマップをtexImage2Dに"); // ! テクスチャ情報の設定 gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); //setFilters(GL10.GL_NEAREST, GL10.GL_NEAREST); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); Log.v("デバッグ","完了"); // ! bitmapを破棄 Log.v("デバッグ","解放処理開始"); if(bitmap !=null){ bitmap.recycle(); bitmap = null; } Log.v("デバッグ","解放処理完了"); //Log.v("デバッグ","ロード完了"); } かなり困っています・・・ どなたかご教授お願いします・・・

    • ベストアンサー
    • Java
  • Androidアプリ開発 画像の回転について

    CANVASに画像を表示し、その画像を回転させるようにしたいのですが、どうにもうまくいかなくて困っています。 画像の中心を軸に回転させたいと思って以下のように実装してみたのですが、うまくいきません。 //////////////////////////////以下抜粋//////////////////////////////// Matrix matrix; // Bitmap のサイズの取得 int width = bmp.getWidth(); int height = bmp.getHeight(); matrix = new Matrix(); // 画像の中心を軸に回転させるようにする(ここがうまくいっていないっぽい・・・) matrix.postRotate(5.0f, width / 2.0f, height / 2.0f); // 回転させた画像を生成する bmp2 = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); // CANVASに画像を描画する canvas.drawBitmap(bmp2 , 10.0f, 10.0f, paint); ////////////////////////////////////////////////////////////////////// 上記のコードで画像自体は回転するのですが、中心軸がずれてしまっているようです。 画像のサイズは横15ピクセル、縦15ピクセルとなっております。 また、matrix.postRotateの前でmatrix.postTranslate()にて軸を設定してみたのですが、 これもうまくいきませんでした。 きれいに画像を回転させるにはどのようにすればよいか、わかる方いましたら教えて いただけないでしょうか?

    • ベストアンサー
    • Java
  • androidで読んだ画像サイズが小さくなります

    アンドロイドで画像を読み込んむと画像サイズが元のサイズより縮小されてしまいます。 画像の元の大きさで読み込むことはできないでしょうか? どなかたよろしければ、ご教授下さい。 256×256の画像を読み込み、以下のソースで確認してみました。 Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.画像の名前); Log.d("tag","w="+bmp1.getWidth()); Log.d("tag","h="+bmp1.bmp1.getHeight()); 結果 w=171 h=171 私は w=256 h=256 のまま読み込みして画面に貼り付けたいのですが。。。

  • Androidアプリに関して

    現在menu .xmlを作成しています。Webサイトを参考に作成してるのですが、 android:icon="@drawable/ic_menu_edit" これは階層はどうしたらよいのでしょうか? drawableというフォルダはプロジェクトの直下に置くのでしょうか? それともmenu .xml と同じ階層でしょうか?

    • ベストアンサー
    • Java
  • Androidの画面レイアウトがくずれます

    同じサイズの画像を並べても、シミュレータで表示するとばらばらのサイズになってしまいます。 もう1ヶ月以上解決できなくて困っています。 写真はXMLベースで4つの画像を表示した結果です。 シミュレータでやってもほぼ同じ結果です。 元々の画像では、茶色と緑が同じ幅で、ピンクと青が同じ幅です。4つの画像とも高さは一緒です。 サイズの変更等はしてません。 GraphicalLayoutoは5.1inWVGA、シミュレータはWVGA854、Android2.3.3です ソースです。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/left"/> <LinearLayout android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/left" android:orientation="vertical"> <ImageView android:id="@+id/top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/top"/> <ImageView android:id="@+id/bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bottom"/> </LinearLayout> <ImageView android:id="@+id/right" android:layout_height="wrap_content" android:src="@drawable/right" android:layout_toRightOf="@+id/center"/> </RelativeLayout>

専門家に質問してみよう