gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:07 26 4
gpt4 key购买 nike

我正在尝试以编程方式创建位图,但发现绘制带阴影的 Rect 会忽略传入的颜色 arg。我已将事情简化为这种情况 - 代码只是绘制了一个旋转的蓝色方 block ,它应该有一个灰色的影子,但影子总是蓝色的:

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

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

最佳答案

你已经把你的内部位图做的太小了,它会破坏矩形的边缘。试试这个:

    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);

关于Android Paint setShadowLayer() 忽略其 Paint 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com