2

画像が直線的にフェードするように、画像にアルファ グラデーションを適用するにはどうすればよいですか? 現在、単位幅の長方形を作成し、それを使用して、ループ内でアルファ値が変更されたペイント オブジェクトでビットマップを描画しています。他に思いつかなかったのでそうしました。したがって、よりきちんとした方法が良いでしょう。

Bitmap bitmap = BitmapFactory.decodeStream(is);
Bitmap bmp = Bitmap.createScaledBitmap(bitmap, 100, 151, true));
bitmap.recycle();

Rect Rect1 = new Rect(0, 0, 100, 100);
Rect Rect2 = new Rect(100, 0, 101, 100);

Paint paint = new Paint();

canvas.drawBitmap(bmp, Rect1, Rect1, null);
while (paint.getAlpha() != 0) {
    paint.setAlpha(paint.getAlpha() - 5);
    canvas.drawBitmap(bmp, Rect2, Rect2, paint);
    Rect2.set(Rect2.left + 1, Rect2.top, Rect2.right + 1, Rect2.bottom);
}

このようなもの

ピクチャ アルファ グラデーション

PSライブ壁紙用にこれをやろうとしています。

4

1 に答える 1

9

おそらく、ビューの操作だけで目的の動作を実現できます。そのためにあなたはすべきです:

  • ドローアブル形状を準備する

res / drawable / gradient_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
            android:startColor="#00FFFFFF"
            android:endColor="#FFFFFFFF"
            android:type="linear" />
</shape>
  • レイアウトを定義します:activity_main.xml

    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/photo"
        android:src="@drawable/gradient_shape" />
    

    ここでは、drawable/photoはdrawablesフォルダー内のjpegです。

  • アクティビティに次のコードを追加することを提案する人もいます(実際には、デバイスのネイティブ構成に依存し、現在は3.0を超えるデバイスでは冗長であるようです)。

    public void onAttachedToWindow() {
         super.onAttachedToWindow();
         Window window = getWindow();
         window.setFormat(PixelFormat.RGBA_8888);
     }
    

その後、4.1デバイスで次のことを確認しました。 サンプル画像

グラデーションのように見えます。それがあなたが探しているものかどうかはまだわかりません。

于 2012-08-29T13:29:17.437 に答える