Java 如何从底部设计圆形视图?
我试图设计一个从底部看是圆形的视图,请看图片 我尝试过不同的设计方法,比如XML,但没有成功。我使用XML代码,它是从底部循环,但当我使用任何图像或横幅滑块时,如图所示,它会保持整个视图 现在我正在使用这个XML代码Java 如何从底部设计圆形视图?,java,android,xml,android-layout,android-studio,Java,Android,Xml,Android Layout,Android Studio,我试图设计一个从底部看是圆形的视图,请看图片 我尝试过不同的设计方法,比如XML,但没有成功。我使用XML代码,它是从底部循环,但当我使用任何图像或横幅滑块时,如图所示,它会保持整个视图 现在我正在使用这个XML代码 <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:backgroun
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/bottom_radius"
android:orientation="vertical">
<ss.com.bannerslider.views.BannerSlider
android:id="@+id/bannerSlider"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
输出在XML中是这样的
XML输出看起来和我预期的一样好,但是当我运行程序并在移动设备或模拟器上看到输出时,输出看起来像
请为我提供解决方案,以便我可以设计我想设计的。我将非常感谢你
将
android:background=“@drawable/bottom\u radius”
放在图像上,而不是linearlayout
制作一个可绘制文件。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp"
android:color="@color/colorPrimary"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="150dp"
android:bottomLeftRadius="150dp"
/>
</shape>
然后将其设置为主布局的背景查看课程。让开发人员以多种不同的方式组合两个位图。我们将在下面的中使用PorterDuff模式PorterDuff.mode.DST_
下面是一个简短的演示,演示了如何生成您想要的结果。首先,结果如下:
我们将使用下面的图像,使用
中的PorterDuff模式PorterDuff.mode.DST_,将照片裁剪成我们想要的形状。一旦使用Porter-Duff模式应用,非透明区域将保留在目标位图(我们的照片)中,照片中对应于下面图像的透明像素的像素将被抛出
现在照片已被裁剪,我们将使用下图应用黄色边框。(上面显示的裁剪位图中的黄色边框并未真正使用。重要的是黄色区域中所有像素的alpha值均为1.0。)此图像的尺寸与前一图像相同,但透明区域也包括黄色弧上方的区域。这个可绘制的将简单地绘制在图像的顶部
代码如下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView topImage = findViewById(R.id.bottomImage);
topImage.setImageBitmap(cropAndOverlay(topImage, R.drawable.smile,
R.drawable.smile_transparent));
}
private Bitmap cropAndOverlay(@NonNull ImageView imageView,
@DrawableRes int cropId, @DrawableRes int overlayId) {
// Get the bitmap for the current image.
Bitmap dst = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
// Get the cropping image. This is the Porter-Duff source image.
Bitmap src = Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), Bitmap.Config.ARGB_8888);
Canvas resultCanvas = new Canvas(src);
Drawable drawable = ResourcesCompat.getDrawable(getResources(), cropId, null);
drawable.setBounds(0, 0, resultCanvas.getWidth(), resultCanvas.getHeight());
drawable.draw(resultCanvas);
// Combine the source with the destination bitmap while applying the Porter-Duff mode.
Bitmap resultBitmap = getPorterDuffBitmap(dst, src, PorterDuff.Mode.DST_IN);
resultCanvas.setBitmap(resultBitmap);
// Place the overlay image on the bitmap.
drawable = ResourcesCompat.getDrawable(getResources(), overlayId, null);
drawable.setBounds(0, 0, resultBitmap.getWidth(), resultBitmap.getHeight());
drawable.draw(resultCanvas);
dst.recycle();
return resultBitmap;
}
private Bitmap getPorterDuffBitmap(Bitmap dst, Bitmap src, PorterDuff.Mode mode) {
Bitmap bitmap =
Bitmap.createBitmap(dst.getWidth(), dst.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// Draw the original bitmap (DST)
canvas.drawBitmap(dst, 0, 0, null);
// Draw the mask (SRC) with the specified Porter-Duff mode
Paint maskPaint = new Paint();
maskPaint.setXfermode(new PorterDuffXfermode(mode));
canvas.drawBitmap(src, 0, 0, maskPaint);
return bitmap;
}
}
以及XML:
活动\u main.xml
<LinearLayout
android:id="@+id/mainLayout"
android:background="#b1a8a8"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/bottomImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/photo" />
</LinearLayout>
我在主要活动中介绍了这一点,但在自定义视图中应用这一技术可能会更好。Kurusu,谢谢你给我建议,但效果不好。我尝试了你的代码,它不是曲线。快速而肮脏的方法是将图像下方圆圈外的白色部分放在矩形图像上方。这将导致透支(由于这个原因不是最优的),但应该有效。@Henry,请提供解决方案,我如何实现这一目标,因为我已经在这方面花费了很多时间。
<LinearLayout
android:id="@+id/mainLayout"
android:background="#b1a8a8"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/bottomImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/photo" />
</LinearLayout>