【androidjava】LayerDrawableを使ってDrawableを重ねる

複数のDrawableを重ねて使いたい時は、LayerDrawableが有効です。

重ねるだけなら至ってシンプルで、Drawableの配列を作って中に重ねたい

Drawableを入れていきます。

後はLayerDrawableのコンストラクタに当てはめて表示するだけ。

自分はmarkerで使いたかったので以下のようにしました。


</pre>
<pre>Drawable[] drawables = {
        activity.getResources().getDrawable(R.drawable.analogdown),
        draw
};
LayerDrawable ld = new LayerDrawable(drawables);
mMarker.setIcon(ld);</pre>
<pre>

※こちらの処理はonPostExecuteで作業しています。urlをdrawableにするには

doInBackgroundの処理が別途必要になるので手順を間違いえないようにしましょう。


</pre>
<pre>@Override
protected Bitmap doInBackground(String... strings) {
    Bitmap myBitmap = getBitmapFromURL(strings[0]);
    return myBitmap;
}</pre>
<pre>
</pre>
<pre>public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }
    public static BitmapDrawable convert(Bitmap bitmap)
    {
        return new BitmapDrawable(bitmap);
    }
}</pre>
<pre>
>株式会社シーポイントラボ

株式会社シーポイントラボ

TEL:053-543-9889
営業時間:9:00~18:00(月〜金)
住所:〒432-8003
   静岡県浜松市中央区和地山3-1-7
   浜松イノベーションキューブ 315
※ご来社の際はインターホンで「316」をお呼びください

CTR IMG