私はAndroidのグラフィックスの初心者です(私はAndroidプログラミング自体にかなり精通していますが)。私は私のアプリに "電球"を表示しようとしています。いくつかの値に基づいて、私は電球の "明るさ"を変更したいと思います。Androidグラフィック:ちょうどイメージのアルファ(背景ではない)を変更する
私はこれにPNG画像を使用しました。
<View
android:id="@+id/view2"
android:layout_width="59dp"
android:layout_height="65dp"
android:background="@drawable/lightbulb" />
そして、コードは以下です:
final View normalView = findViewById(R.id.view2);
//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
normalView.invalidate();
PNGファイルは黒で、電球の輪郭のみと透明グラフィックです。ここで、アルファを低い値(0に近い値)に設定すると、グラフィック全体が透明になります。私は球の内容が透明になることをだけにします。球根の輪郭が見えてくれることを願っています。
カスタムビューを次のように作成しようとしましたが、グラフィックが表示されません。
class LightbulbView extends View {
private BitmapDrawable mLightbulbDrawable;
private Paint mPaint;
/*All constructors which call through to super*/
private void initialize(){
mLightbulbDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.lightbulb));
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mLightbulbDrawable.draw(canvas);
}
/*Without this, I get an NPE when I do a getBackground on this view*/
@Override
public Drawable getBackground() {
return mLightbulbDrawable;
//return super.getBackground();
}
}
もちろん、ImageViewを使用しても問題ありません。結果はまったく同じです。
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:src="@drawable/lightbulb"
></ImageView>
そしてここでは、コード
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAlpha(alphaValue);
はそうだ、アンドロイドでこれを行うための最善の方法は何ですか?
SOLUTION
これは、私はそれをやった方法です。詳細は、(@Petrusで)受け入れ答えをご覧ください。
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="wrap_content"
android:src="@drawable/lightbulb_content"
android:layout_width="wrap_content"
android:id="@+id/bulbContent"
></ImageView>
<ImageView
android:layout_height="wrap_content"
android:src="@drawable/lightbulb"
android:layout_width="wrap_content"
android:id="@+id/bulb"
></ImageView>
</FrameLayout>
、コードで:
ここfinal ImageView bulbContent = (ImageView) findViewById(R.id.bulbContent);
//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
bulbContent.setAlpha(alphaValue);
、lightbulb.pngは電球のちょうど概要と画像です。 lightbulb_content.pngは、球の "内容"を持つイメージです。
パーフェクト!それがトリックでした。ありがとう。 – curioustechizen