「Xamarin.AndroidでViewをBitmapとして取得する」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator (ページの作成:「Xamarin.AndroidでViewをBitmapとして取得する <source> // Viewを取得する var view = FindViewById<ImageView>(Resource.Id.imageView1); // Bitmap...」) |
imported>Administrator |
||
59行目: | 59行目: | ||
var root = FindViewById<LinearLayout>(Resource.Id.rootLayout1); | var root = FindViewById<LinearLayout>(Resource.Id.rootLayout1); | ||
− | button.Click += (sender, e) => { | + | button.Click += (sender, e) => |
+ | {// ここから | ||
+ | |||
+ | // キャッシュ取得を有効にする | ||
root.DrawingCacheEnabled = true; | root.DrawingCacheEnabled = true; | ||
+ | // キャッシュを破棄する | ||
+ | root.DestroyDrawingCache(); | ||
+ | // キャッシュを生成する | ||
var cache = root.DrawingCache; | var cache = root.DrawingCache; | ||
if (cache == null) | if (cache == null) | ||
66行目: | 72行目: | ||
return; | return; | ||
} | } | ||
+ | // キャッシュからビットマップを生成する | ||
var bitmap = Android.Graphics.Bitmap.CreateBitmap(cache); | var bitmap = Android.Graphics.Bitmap.CreateBitmap(cache); | ||
+ | // キャッシュ取得を無効にする | ||
root.DrawingCacheEnabled = false; | root.DrawingCacheEnabled = false; | ||
2018年3月1日 (木) 02:17時点における最新版
Xamarin.AndroidでViewをBitmapとして取得する
// Viewを取得する
var view = FindViewById<ImageView>(Resource.Id.imageView1);
// Bitmap化
view.DrawingCacheEnabled = true;
var cache = view.DrawingCache;
if (cache == null)
{
return;
}
var bitmap = Android.Graphics.Bitmap.CreateBitmap(cache);
view.DrawingCacheEnabled = false;
スクリーンショットを取る[編集 | ソースを編集]
LinearLayoutやFrameLayoutなどの「ほげほげLayout」もViewの派生クラスなので、.axmlファイルでルート要素となっているLayoutをBitmap化すればスクリーンショットを取ることもできる。 Android 5.0から使えるMediaProjection APIによるスクリーンショットと異なりアプリ外までは取得できないが、この方法は非常にシンプルでパーミッションも不要という利点がある。
- Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
[Activity(Label = "screenshot", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
//
var button = FindViewById<Button>(Resource.Id.button1);
var imageView = FindViewById<ImageView>(Resource.Id.imageView1);
var root = FindViewById<LinearLayout>(Resource.Id.rootLayout1);
button.Click += (sender, e) =>
{// ここから
// キャッシュ取得を有効にする
root.DrawingCacheEnabled = true;
// キャッシュを破棄する
root.DestroyDrawingCache();
// キャッシュを生成する
var cache = root.DrawingCache;
if (cache == null)
{
return;
}
// キャッシュからビットマップを生成する
var bitmap = Android.Graphics.Bitmap.CreateBitmap(cache);
// キャッシュ取得を無効にする
root.DrawingCacheEnabled = false;
imageView.SetImageBitmap(bitmap);
};
}
}