Saturday, January 08, 2011

Android/ グラデーションを描画する(GradientDrawable)

前回、SurfaceViewでの描画でつまずいたに続きまして、
またまたAndroidの描画でつまずいてみましたのでご報告。

[PROBLEM: NO IDEA TO DRAW GRADATION]
【つまずき】
グラデーションをどのように描画すればいいのですか?

【解決プロセス】
GradientDrawableを使えば良さそうなことはさすがにドキュメントからわかりました。
でも、いろんな開発者が「これじゃわかんないよ」と言っているようで、僕にはこのドキュメントから使い方を読み解くことはできませんでした。
そこで、こちらを参考にしました。http://softwareobjects.net/technology/other/how-to-create-android-live-wallpaper/

【先に結果をば】
[RESULT: PASSED]


【コードと解決手段】
[SOLUTION: USE GradientDrawable]
手順1.GradientDrawableを作る(その時、グラデーション方向や色を指定)
手順2.GradientDrawableで描画する領域の境界をRect型で指定する
手順3.GradientDrawableのdraw関数を呼び出して描画する

[FYI: HERE IS A SAMPLE CODE]
コードは下記です。

GradientDrawable mGradient = new GradientDrawable(
Orientation.LEFT_RIGHT,
new int[] { 0xff303030, 0xff777777 });
mGradient.setBounds(new Rect(0,0,getWidth()/2,getHeight()));
mGradient.draw(canvas);

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. If you don't see any bugs or if you can't determine
    the reason why the plant is looking sick, take a sample in to the local
    county extension office. Native plants or at least plants adapted to similar conditions as your region will have a better chance of growing and thriving.

    Grab a few bags of organic potting soil and lean them up against
    the sunny side of your garage or a brick wall of your home.


    Have a look at my blog post; bedeck

    ReplyDelete