如何只在侧面适应ImageView的角落以适应Android中的父级布局?

如何只在侧面适应ImageView的角落以适应Android中的父级布局?,第1张

概述我是Android的初学者.现在,我正在学习如何正确设计AndroidApp.但是我对ImageView有问题.我想绕过ImageView的各个角落,但仅限于一侧.该图像是我想要得到的:如您所见,只有ImageView的左角是四舍五入的以适合父视图.这就是我现在得到的:这是我的列表视图行的XML布局:<?xmlvers

我是Android的初学者.现在,我正在学习如何正确设计AndroID App.但是我对ImageVIEw有问题.我想绕过ImageVIEw的各个角落,但仅限于一侧.

该图像是我想要得到的:

如您所见,只有ImageVIEw的左角是四舍五入的以适合父视图.

这就是我现在得到的:

这是我的列表视图行的XML布局:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:background="@drawable/List_row_bg"    androID:orIEntation="horizontal"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <linearLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content">        <ImageVIEw            androID:background="@color/colorPrimary"            androID:ID="@+ID/row_image"            androID:scaleType="fitCenter"            androID:layout_wIDth="100dp"            androID:layout_height="100dp" />    </linearLayout>    <linearLayout        androID:padding="@dimen/List_item_content_padding"        androID:orIEntation="vertical"        androID:layout_weight="1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content">        <TextVIEw            androID:ID="@+ID/row_Title"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content" />        <TextVIEw            androID:ID="@+ID/row_duration"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content" />        <TextVIEw            androID:ID="@+ID/row_file_size"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content" />        <TextVIEw            androID:ID="@+ID/row_description"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content" />    </linearLayout></linearLayout>

我遵循了this tutorial.但是ImageVIEw包含了填充.但是,当我删除填充时,它无法正常工作.

解决方法:

我认为您应该在加载图像时应用转换.例如,如果您使用picasso加载图像,则可以应用此转换:

基类:

public class RoundedCornersBitmap implements transformation {private static final float DEFAulT_RADIUS = 5.f;private static final int DEFAulT_border_color = color.WHITE;private static final int DEFAulT_stroke_WIDTH = 3;protected float mCornerRadius;protected int mbordercolor;protected int mstrokeWIDth;@OverrIDepublic String key() {    return "roundedCorners()";}public RoundedCornersBitmap() {    mCornerRadius = DEFAulT_RADIUS;    mbordercolor = DEFAulT_border_color;    mstrokeWIDth = DEFAulT_stroke_WIDTH;}public RoundedCornersBitmap(float cornderRadius, int bordercolor, int strokeWIDth) {    mCornerRadius = cornderRadius;    mbordercolor = bordercolor;    mstrokeWIDth = strokeWIDth;}@OverrIDepublic Bitmap transform(Bitmap bitmap) {    Bitmap output = Bitmap.createBitmap(bitmap.getWIDth(), bitmap.getHeight(), Config.ARGB_8888);    Canvas canvas = new Canvas(output);    final Paint paint = new Paint();    final Rect rect = new Rect(mstrokeWIDth, mstrokeWIDth, (bitmap.getWIDth() - mstrokeWIDth), bitmap.getHeight()            - mstrokeWIDth);    final RectF rectF = new RectF(0, 0, bitmap.getWIDth(), bitmap.getHeight());    paint.setAntiAlias(true);    paint.setcolor(mbordercolor);    paint.setstrokeWIDth(3);    canvas.drawARGB(0, 0, 0, 0);    canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);    //canvas.drawCircle(bitmap.getWIDth() / 2, bitmap.getHeight() / 2, bitmap.getWIDth() / 2, paint);    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    canvas.drawBitmap(bitmap, rect, rect, paint);    bitmap.recycle();    return output;}/** * @return the mCornerRadius */public float getCornerRadius() {    return mCornerRadius;}/** * @param mCornerRadius *            the mCornerRadius to set */public voID setCornerRadius(float mCornerRadius) {    this.mCornerRadius = mCornerRadius;}/** * @return the mbordercolor */public int getbordercolor() {    return mbordercolor;}/** * @param mbordercolor *            the mbordercolor to set */public voID setbordercolor(int mbordercolor) {    this.mbordercolor = mbordercolor;}/** * @return the mstrokeWIDth */public int getstrokeWIDth() {    return mstrokeWIDth;}/** * @param mstrokeWIDth *            the mstrokeWIDth to set */public voID setstrokeWIDth(int mstrokeWIDth) {    this.mstrokeWIDth = mstrokeWIDth;}}

leftRoundedCornersBitmap:

public class leftRoundedCornersBitmap extends RoundedCornersBitmap {    public leftRoundedCornersBitmap() {        super();    }    public leftRoundedCornersBitmap(float cornderRadius, int bordercolor, int strokeWIDth) {        super(cornderRadius, bordercolor, strokeWIDth);    }    @OverrIDe    public Bitmap transform(Bitmap bitmap) {        Bitmap output = Bitmap.createBitmap(bitmap.getWIDth(), bitmap.getHeight(), Config.ARGB_8888);        Canvas canvas = new Canvas(output);       final Paint paint = new Paint();       final Rect rect = new Rect(mstrokeWIDth, mstrokeWIDth, (bitmap.getWIDth() - mstrokeWIDth), bitmap.getHeight()            - mstrokeWIDth);       final RectF rectF = new RectF(rect);       final Rect topRightRect = new Rect(bitmap.getWIDth() / 2, 0, bitmap.getWIDth(), bitmap.getHeight() / 2);       final Rect bottomrect = new Rect(bitmap.getWIDth() / 2, bitmap.getHeight() / 2, bitmap.getWIDth(), bitmap.getHeight());       paint.setAntiAlias(true);       paint.setcolor(mbordercolor);       paint.setstrokeWIDth(3);       canvas.drawARGB(0, 0, 0, 0);       canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);       canvas.drawRect(topRightRect, paint);       canvas.drawRect(bottomrect, paint);       paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));       canvas.drawBitmap(bitmap, rect, rect, paint);       bitmap.recycle();       return output;   }}

gradle文件中的毕加索依赖关系:

compile 'com.squareup.picasso:picasso:2.5.2'

例:

Picasso.with(context).load(imageUrl).transform(new leftRoundedCornersBitmap()).into(youImageVIEw);
总结

以上是内存溢出为你收集整理的如何只在侧面适应ImageView的角落以适应Android中的父级布局?全部内容,希望文章能够帮你解决如何只在侧面适应ImageView的角落以适应Android中的父级布局?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存