AndroidでImageViewに大きなサイズの画像を非同期で読み込みます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample32-006-ImageViewBigImageLoadBackground」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample32_006 |
| プロジェクト名(Project Name) | AndroidSample32-006-ImageViewBigImageLoadBackground |
| パッケージ名(Package Name) | com.example.androidsample32_006 |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample32_006"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidsample32_006.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「任意のディレクトリの作成方法」の手順で「res/drawable」ディレクトリを作成。
2.「画像」を「res/drawable」に「android_sample_image_5000_5000.png」という名前で保存。
【手順4】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton"
android:text="ボタン" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「src/com/example/androidsample32_006/MainActivity.java」は以下の通り。
package com.example.androidsample32_006;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickButton(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
BitmapWorkerTask task = new BitmapWorkerTask(this, imageView);
task.execute(R.drawable.android_sample_image_5000_5000);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「Javaクラスファイルの作成方法」の手順で「src/com/example/○○/」ディレクトリに「BitmapWorkerTask.java」というファイルを作成。
2.「src/com/example/androidsample32_006/BitmapWorkerTask.java」は以下の通り。
package com.example.androidsample32_006;
import java.lang.ref.WeakReference;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private Context context;
private ImageView imageView;
private WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(Context context, ImageView imageView) {
this.context = context;
this.imageView = imageView;
this.imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(Integer... params) {
int resourceId = params[0];
return decodeSampledBitmapFromResource(context.getResources(),
resourceId,
imageView.getWidth(),
imageView.getHeight());
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
private Bitmap decodeSampledBitmapFromResource(Resources res, int resourceId, int requestWidth, int requestHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resourceId, options);
options.inSampleSize = calculateSampleSize(options, requestWidth, requestHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resourceId, options);
}
private int calculateSampleSize(BitmapFactory.Options options, int requestWidth, int requestHeight) {
int height = options.outHeight;
int width = options.outWidth;
int sampleSize = 1;
if (height > requestHeight || width > requestWidth) {
int heightRatio = Math.round((float) height / (float) requestHeight);
int widthRatio = Math.round((float) width / (float) requestWidth);
if (heightRatio < widthRatio) {
sampleSize = heightRatio;
} else {
sampleSize = widthRatio;
}
}
return sampleSize;
}
}
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順7】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順8】
1.以下の様に表示されれば成功です。
以上です。



0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。