2012年04月15日(日)
2D 描画 API
|で、上の文書によると以下の方法があります。(以下、抜粋引用)
- a. Draw your graphics or animations into a View object from your layout.
- b. Draw your graphics directly to a Canvas.
Option "a," drawing to a View, is your best choice when you want to draw simple graphics that do not need to change dynamically and are not part of a performance-intensive game.
Option "b," drawing to a Canvas, is better when your application needs to regularly re-draw itself.
「drawing to a View」方法
こちらのブログで、とても分かりやすく説明されていました。
要点はこんな感じ。(上のブログ読んでやっと分かりました。。。)
public class CustomView extends View { // View を作る : @Override public void onDraw(Canvas canvas) { : canvas.xxxx(..., ... ); // Canvas に描画 : canvas.yyyy(..., ... ); : } : } public class CustomActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new CustomView(this)); // View を設定 } :
Android Developers に載っていた例
Drawable を取得した場合とどう等価かわからなかったのでメモ。
public class DrawableSampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout mLinearLayout = new LinearLayout(this); ImageView i = new ImageView(this); i.setImageResource(R.drawable.my_image); // 上の行は、以下のコードと等価 // -------- // Resources res = getResources(); // Drawable myImage = res.getDrawable(R.drawable.my_image); // i.setImageDrawable(myImage); // -------- i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mLinearLayout.addView(i); setContentView(mLinearLayout); } }
画像ファイルは res/drawable/ に置く。
my_image.gif を置いても ID (の一部)になるのはベース名(my_image)のみ。
サポートされる画像形式は、JPEG(.jpg)、PNG(.png)、GIF(.gif)、NinePatch(.9.png)。
----
XML で書いた場合の例
android:contentDescription が無いと warning が出ます。
<ImageView android:contentDescription="@string/image_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tint="#55ff0000" android:src="@drawable/my_image"/>
コメント