2016-10-18 2 views
0

最初のアプリケーションを作成しようとしていますが、画像の使用に問題があります。私はアプリで表示する画像を取得することができますが、高解像度のために読み込み時間が遅いことがわかりました。私はビットマップを追加し、developers.android.comからサンプルコードを見つけましたが、実装した時点でイメージは見えなくなりました。画像ビューは、それのすぐ下にあるように設定されたテキストが適切な場所にあるので、私の知る限り、まだそこにあります。Android Studioでビットマップを使用しているときに画像が表示されない

私は、ビットマップコードを正しく実装する場所が不明であり、間違ったクラスファイルに含まれている可能性があることを説明します。画像が示されている

アクティビティー:活性の

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_ion" 
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="com.example.michael.titanfall2apptest.Ion"> 

<ImageView 
    app:srcCompat="@drawable/ion" 
    android:id="@+id/ionimageView" 
    android:layout_centerHorizontal="true" 
    android:layout_width="175dp" 
    android:layout_height="175dp"/> 

<TextView 
    android:text="Ion" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/ionimageView" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/textView"/> 
</RelativeLayout> 

クラス:任意のリソースの提案も理解されるであろう

public class Ion extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_ion); 
    ImageView ionImageView = (ImageView) findViewById(R.id.ionimageView); 
    ionImageView.setImageBitmap(
      decodeSampledBitmapFromResource(getResources(),  R.id.ionimageView, 500, 500)); 
} 

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 
    Log.d("test", "Look here"); 
    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
                int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

。私はこれを正確に説明しているビデオは見つけられていません。

答えて

0

あなたはイメージのための唯一のコンテナですImageViewの、のIDを渡し、ので、起こる: ionImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.ionimageView, 500, 500));

あなたが描画イメージ(ないImageViewの)のIDを渡す必要があります。また、 ionImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.ion, 500, 500));

削除:

app:srcCompat="@drawable/ion" 

+0

これは意味があります。私はR.drawableの代わりにR.idを使用しているのを見ました。それは今Drawableのイオンを認識しますが、このようなImageView ionImageView =(ImageView)findViewById(R.drawable.ion);というイメージビューを作成すると、それはidのリソースタイプ、ionのidを期待していると言います。ビューを間違って作成していますか? –

+0

はい、間違っています。 findViewById()メソッドでは、ビューのID(あなたの場合はImageView)を使用します。 R.id.ionimageView。 drawable useの場合drawable id:R.drawable.ion。 – Hobbajt

+0

私はまだ問題があります。画像や画像を渡すことになっているのであれば混乱しています。私がするなら、ImageView ionImageView =(ImageView)findViewById(R.drawable.ion2); ion2は描画可能ではないので、私は叫ぶ。だから私はここで2つの異なることをしようとしていると誤った構文を持っていると思う。 –

関連する問題