我在卡片视图中有约束布局。卡片视图在屏幕上看起来不错。仅当截取屏幕截图时,它才会被渲染为位图,即失去角点。我添加了位图视图的图片。约束布局与圆角重叠。我需要边到边的圆度。
下面是正确显示圆角的模拟器的屏幕截图:
这是渲染为位图的视图:
下面是我的示例代码。
public class MainActivity extends AppCompatActivity {
private CardView cardView;
private Button button;
private ConstraintLayout parentLayoutA;
private ConstraintLayout layoutB;
private ConstraintLayout childLayout;
private ConstraintSet innerSet;
private ConstraintSet outerSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CreateCardViewProgrammatically();
}
public void CreateCardViewProgrammatically(){
cardView = new CardView(this);
childLayout = new ConstraintLayout(this);
parentLayoutA = new ConstraintLayout(this);
layoutB = new ConstraintLayout(this);
button = new Button(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
layoutB.setClipToOutline(true);
}
cardView.setRadius(150);
button.setText("Click!!");
button.setTextColor(Color.BLACK);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takeScreenShot();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
parentLayoutA.setId(View.generateViewId());
layoutB.setId(View.generateViewId());
childLayout.setId(View.generateViewId());
cardView.setId(View.generateViewId());
button.setId(View.generateViewId());
}
innerSet = new ConstraintSet();
outerSet = new ConstraintSet();
ConstraintLayout.LayoutParams parentParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.MATCH_PARENT
);
layoutB.setLayoutParams(parentParams);
childLayout.setLayoutParams(parentParams);
cardView.setCardElevation(0);
cardView.setPadding(50,50,50,50);
parentLayoutA.addView(layoutB);
layoutB.addView(cardView);
cardView.addView(childLayout);
childLayout.addView(button);
innerSet.centerHorizontally(button.getId(), ConstraintSet.PARENT_ID);
innerSet.centerVertically(button.getId(), ConstraintSet.PARENT_ID);
innerSet.constrainHeight(button.getId(), 200);
outerSet.connect(cardView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 200);
outerSet.connect(cardView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 200);
outerSet.centerHorizontally(cardView.getId(), ConstraintSet.PARENT_ID);
outerSet.centerVertically(cardView.getId(), ConstraintSet.PARENT_ID);
outerSet.constrainHeight(cardView.getId(), 1500);
innerSet.applyTo(childLayout);
outerSet.applyTo(layoutB);
setContentView(parentLayoutA);
parentLayoutA.setBackgroundColor(Color.YELLOW);
layoutB.setBackgroundColor(Color.MAGENTA);
cardView.setCardBackgroundColor(Color.GREEN);
childLayout.setBackgroundColor(Color.BLUE);
}
public void takeScreenShot() {
imageFromView(parentLayoutA);
}
public Bitmap imageFromView(View view) {
DisplayMetrics metrics = this.getApplication().getResources().getDisplayMetrics();
int width = view.getWidth() > 0 ? view.getWidth() : metrics.widthPixels;
int height = view.getHeight() > 0 ? view.getHeight() : metrics.heightPixels;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}