Android Paint setShadowLayer() 忽略其 Paint 的颜色

发布于 2024-12-02 04:41:55 字数 2161 浏览 1 评论 0原文

我正在尝试以编程方式创建位图,但发现绘制阴影矩形会忽略传入的颜色参数。我已将事情简化为这种情况 - 代码仅绘制一个旋转的蓝色正方形,该正方形应该具有灰色阴影,但阴影始终是蓝色的:

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:padding="40dp"
    >
</LinearLayout>

RotateRectShadowActivity.java:
public class RotateRectShadowActivity extends Activity {
    private LinearLayout mMainLayout;
    private ImageView mImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater factory = LayoutInflater.from( this );
        mMainLayout = (LinearLayout) factory.inflate( R.layout.main, null );

        mImageView = new ImageView(this);

        Rect rect = new Rect(0, 0, 300, 300);

        Bitmap outerBm = Bitmap.createBitmap(
                rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
        Bitmap innerBm = Bitmap.createBitmap(
                rect.width()-50, rect.height()-50, Bitmap.Config.ARGB_8888);

        Canvas outerCanvas = new Canvas(outerBm);
        Canvas innerCanvas = new Canvas(innerBm);
        outerCanvas.rotate(3);

        rect.left += 25;
        rect.top += 25;
        rect.right += 25;
        rect.bottom += 25;

        Paint shadowPaint = new Paint();
        shadowPaint.setShadowLayer(12, 12, 12, 0xFF555555);
        shadowPaint.setColor(0xFF555555);

        innerCanvas.drawRect(rect, shadowPaint);

        Paint rectPaint = new Paint();
        rectPaint.setColor(Color.BLUE);
        innerCanvas.drawRect(rect, rectPaint);
        outerCanvas.drawBitmap(innerBm, 0, 0, shadowPaint);

        mImageView.setImageBitmap(outerBm);
        mMainLayout.addView(mImageView);
        setContentView(mMainLayout);
    }
}

RotateRectShadow

我不明白为什么要调用outCanvas.drawBitmap(innerBm, 0, 0, ShadowPaint);根本需要一个 Paint,因为我试图通过innerCanvas 在innerBm 本身上绘制阴影,但是当我传递 null 时,根本没有阴影。

I'm trying to create a Bitmap programmatically and am finding drawing a shadowed Rect is ignoring the color arg passed in. I've simplified things down to this case - the code just draws a rotated blue square which is supposed to have a grey shadow, but the shadow is always blue:

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:padding="40dp"
    >
</LinearLayout>

RotateRectShadowActivity.java:
public class RotateRectShadowActivity extends Activity {
    private LinearLayout mMainLayout;
    private ImageView mImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater factory = LayoutInflater.from( this );
        mMainLayout = (LinearLayout) factory.inflate( R.layout.main, null );

        mImageView = new ImageView(this);

        Rect rect = new Rect(0, 0, 300, 300);

        Bitmap outerBm = Bitmap.createBitmap(
                rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
        Bitmap innerBm = Bitmap.createBitmap(
                rect.width()-50, rect.height()-50, Bitmap.Config.ARGB_8888);

        Canvas outerCanvas = new Canvas(outerBm);
        Canvas innerCanvas = new Canvas(innerBm);
        outerCanvas.rotate(3);

        rect.left += 25;
        rect.top += 25;
        rect.right += 25;
        rect.bottom += 25;

        Paint shadowPaint = new Paint();
        shadowPaint.setShadowLayer(12, 12, 12, 0xFF555555);
        shadowPaint.setColor(0xFF555555);

        innerCanvas.drawRect(rect, shadowPaint);

        Paint rectPaint = new Paint();
        rectPaint.setColor(Color.BLUE);
        innerCanvas.drawRect(rect, rectPaint);
        outerCanvas.drawBitmap(innerBm, 0, 0, shadowPaint);

        mImageView.setImageBitmap(outerBm);
        mMainLayout.addView(mImageView);
        setContentView(mMainLayout);
    }
}

RotateRectShadow

I don't see why the call to outerCanvas.drawBitmap(innerBm, 0, 0, shadowPaint); needs a Paint at all, since I'm trying to draw the shadow on the innerBm itself, via innerCanvas, but when I pass null in there's no shadow at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-12-09 04:41:55

您将内部位图设置得太小,并且它会影响矩形的边缘。试试这个:

    Bitmap outerBm = Bitmap.createBitmap( 
            rect.width(), rect.height(), Bitmap.Config.ARGB_8888); 
    Bitmap innerBm = Bitmap.createBitmap( 
            rect.width() + 50, rect.height() + 50, Bitmap.Config.ARGB_8888); 

    Canvas outerCanvas = new Canvas(outerBm); 
    Canvas innerCanvas = new Canvas(innerBm); 
    outerCanvas.rotate(3); 

    rect.left += 25; 
    rect.top += 25; 
    rect.right -= 25; 
    rect.bottom -= 25; 

    Paint shadowPaint = new Paint();
    shadowPaint.setColor(Color.BLUE);; 
    shadowPaint.setShadowLayer(12, 12, 12, 0xFF555555); 

    innerCanvas.drawRect(rect, shadowPaint); 

    outerCanvas.drawBitmap(innerBm, 0, 0, null); 

You have made your inner bitmap too small and it custs the edges of the rectange. Try this:

    Bitmap outerBm = Bitmap.createBitmap( 
            rect.width(), rect.height(), Bitmap.Config.ARGB_8888); 
    Bitmap innerBm = Bitmap.createBitmap( 
            rect.width() + 50, rect.height() + 50, Bitmap.Config.ARGB_8888); 

    Canvas outerCanvas = new Canvas(outerBm); 
    Canvas innerCanvas = new Canvas(innerBm); 
    outerCanvas.rotate(3); 

    rect.left += 25; 
    rect.top += 25; 
    rect.right -= 25; 
    rect.bottom -= 25; 

    Paint shadowPaint = new Paint();
    shadowPaint.setColor(Color.BLUE);; 
    shadowPaint.setShadowLayer(12, 12, 12, 0xFF555555); 

    innerCanvas.drawRect(rect, shadowPaint); 

    outerCanvas.drawBitmap(innerBm, 0, 0, null); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文