Androidアプリを作ろう08 ウィジェットの応用②
ウィジェットに画像を表示させるアプリ
実機があると便利です
目次
任意の画像を表示させるウィジェットアプリ
今回は、フォトから画像を取り込み、ウィジェットに貼り付けるアプリを製作します。
設定は前回の通りに行ない、ウィジェットクラスとアクティビティクラスの双方を作成しましょう
NewAppWidgetクラス
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; /** * Implementation of App Widget functionality. */ public class NewAppWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText); Intent intent = new Intent(context,MainActivity.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId); PendingIntent pIntent = PendingIntent.getActivity(context,appWidgetId,intent,0); views.setOnClickPendingIntent(R.id.appwidget_text,pIntent); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // There may be multiple widgets active, so update all of them for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId); } } @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created } @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } } |
MainActivityクラス
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RemoteViews; import java.io.InputStream; public class MainActivity extends AppCompatActivity { public static final int PHOTO_APP = 1; Button button; ImageView view; int id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); button = new Button(this); view = new ImageView(this); button.setText("画像の選択"); layout.addView(view); layout.addView(button); Intent intent = getIntent(); Bundle extra = intent.getExtras(); id = extra.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent,PHOTO_APP); } }); } @Override public void onActivityResult(int request,int result,Intent intent){ if(request == PHOTO_APP && result == RESULT_OK){ try{ Uri uri = intent.getData(); InputStream stream = getContentResolver().openInputStream(uri); Bitmap bmp = BitmapFactory.decodeStream(stream); Context context = getBaseContext(); RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.new_app_widget); views.setViewVisibility(R.id.appwidget_text,View.GONE); views.setImageViewBitmap(R.id.imageView,bmp); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(id,views); fileList(); }catch(Exception e){} } } } |
ImageViewをウィジェットに追加する
これはデフォルトで出来上がっている「new_app_widget」が以下のようになるようコードを追加してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/widget_margin" android:background="#09C"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:layout_above="@+id/appwidget_text" android:layout_alignEnd="@+id/appwidget_text" android:layout_marginEnd="83dp" android:layout_marginBottom="76dp" android:id="@+id/imageView"/> <TextView android:id="@+id/appwidget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/appwidget_text" android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic" android:layout_margin="8dp" android:contentDescription="@string/appwidget_text" android:background="#09C"/> </RelativeLayout> |
今回、クラス名はデフォルトのまま変更せずに作成しました。
実行結果は以下のようになります。
※画像はテストのため適当な画像をフォトフォルダに入れています。
プログラムの解説
前回と同様にウィジェットをクリックしたときにアクティビティを起動するようにしています。
このアクティビティは標準アプリであるフォトを起動するようにしています。
フォトを起動するアプリについては、「データの共有②」を参照してください。
選択が完了したら、返された結果をもとにアクティビティ側で画像をウィジェットに設定しています。
さいごに
このアプリは実機だと環境により実行結果に差異が出てきますので注意してください。
次回から新章に入ります。