2013年2月23日土曜日

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

結論:含まれる

親View{
 子View
 子View
 ...


上記関係にあるViewにおいて、親ViewでgetDrawingCache()して取得したBitmapには子Viewの画像も描かれている。
実験に使ったコードは下記のとおり。

public class MainActivity extends Activity
    implements
    OnClickListener
    {
    Button btn1, btn2;
    Bitmap bm;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.Button1);
        btn1.setOnClickListener(this);
        btn2 = (Button)findViewById(R.id.Button2);
        btn2.setOnClickListener(this);
        bm = null;
    }

    @Override
    protected void onDestroy() {
        if(bm!=null) bm.recycle();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if(bm!=null){
            bm.recycle();
            bm = null;
        }
     
        if(v==btn1) v = (RelativeLayout)findViewById(R.id.RelativeLayout);
        else{
            if(v==btn2) v = (TextView)findViewById(R.id.TextView);
            else v = null;
        }
        if(v!=null){
            v.setDrawingCacheEnabled(true);
            bm = v.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
            v.setDrawingCacheEnabled(false);
            ImageView iv = (ImageView)findViewById(R.id.Image2);
            iv.setImageBitmap(bm);
        }
    }
}

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:id="@+id/RelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9999ff"
        >
        <ImageView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:contentDescription="@string/app_name"
            android:background="#99ff99"
            >
        </ImageView>
        <Button
            android:id="@+id/Button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:text="@string/Button1" 
            >
        </Button>
        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:layout_below="@id/Button1"
            android:gravity="center"
            android:text="@string/hello_world" 
            android:background="#ff9999"
        />
    </RelativeLayout>
    <Button
        android:id="@+id/Button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button2" 
        >
    </Button>
    <ImageView
        android:id="@+id/Image2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/app_name"
        >
    </ImageView>
</LinearLayout>


2013年2月16日土曜日

NoClassDefFoundError

NoClassDefFoundError

NoClassDefFoundErrorに関する仕掛けは、次のサイトに書かれてある。
Androidアプリ実行時にNoClassDefFoundError
実行時にクラスパスが通ってない場合に発生するそうです。

私の場合は、Libsフォルダー内に、本来あるべきjarファイルを復活させたら治りました。

発生原因は、Libsフォルダー内の、何かのjarファイルを誤って削除してしまっていたことのようです。

2013年2月15日金曜日

Access Not Configured

Access Not Configured

detailMessage "403 Forbidden\n{\n  "code" : 403,\n  "errors" : [ {\n    "domain" : "usageLimits",\n    "message" : "Access Not Configured",\n    "reason" : "accessNotConfigured"\n  } ],\n  "message" : "Access Not Configured"\n}" (id=xxxxxxxxxx)

上記エラーに出会うかもしれない。もし、出会ったなら、keyの作成に問題があったのかもしれない。
APIs Consoleにアクセスして、
API Access>Simple API Access>Delete key...>Delete key
Create new Android Key...>空欄には何も記入せず>Create
これで再度チャレンジしてみよう。

2013年2月10日日曜日

BitmapFactory.Options inSampleSize

BitmapFactory.Options inSampleSize

私は、inSampleSizeに3を代入すれば、(Bitmapの大きさが)3分の1になるものと思い込んでいました。しかし、挙動が変なので、調べてみたのです。

BitmapFactory.Options inSampleSizeに関しては、次のサイトで動作検証がなされています。
BitmapFactory.Options inSampleSize の性能テストとか動作とか
貴重なデータを公開して頂きありがとうございます。手間が省けました。
上記サイトでの検証結果では、inSampleSizeに3を代入しても(Bitmapの大きさが)2分の1になってしまうのです。むむむ!

検証結果からすると、inSampleSizeに関して次のように解釈できます。
inSampleSizeを用いて縮小する場合、inSampleSizeに代入されている値の小さい方の直近の「2の累乗の倍数」で縮小される。
例えば、inSampleSizeに10を代入したとしても、(10分の1になるのではなく)8分の1に縮小されるのです。

念のため、inSampleSizeに関する留意点を書いておきます。

  1. inSampleSizeに関する仕掛けは、メモリの節約を目的としている。
  2. inSampleSizeには、縮小する倍率を代入する。例えば、2分の1に縮小したい場合には2を代入する。
  3. inSampleSizeは、int型であるため(float型では無いという意味で)大雑把な縮小しかできない。
  4. inSampleSizeは、上記の検証結果のとおり、大雑把な縮小しかできない。
  5. (inSampleSizeを使って作成された)この大雑把に縮小されたBitmapに対して、必要に応じて、本来目的としていたサイズに(更に微調整して)縮小する。
  6. 縮小する大きさが2分の1よりも大きい場合(例えば、3分の2の大きさに縮小したい場合)、inSampleSizeに関する仕掛けを使うことに意味は無い。この場合、別の仕掛けで縮小する。