当前位置: 首页 > 知识库问答 >
问题:

Android-ImageView多位图

车嘉实
2023-03-14

我正在开发一个聊天应用程序,它确实需要显示与我聊天的每个人的个人资料图片。如果是一个群组对话,我需要设计一个类似于fb的布局,如下所示

我正在考虑使用layerdrawable实现它,但不确定如何实现。此外,还需要从server加载映像。我正在使用Glide库加载图像

共有1个答案

苍嘉澍
2023-03-14

考虑了3个布局并排,并将顶部布局包装成圆形。

<?xml version="1.0" encoding="utf-8"?>
<nz.co.example.components.CircleLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="@dimen/messaging_profile_pic_size"
android:layout_height="@dimen/messaging_profile_pic_size"
android:orientation="horizontal"
custom:diameter="@dimen/messaging_profile_pic_size"
>
<ImageView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/iv_left"
    android:scaleType="centerCrop"
    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingStart="@dimen/line_spacing"
    android:paddingEnd="0dp"
    >
    <ImageView
        android:layout_width="@dimen/messaging_profile_pic_half_size"
        android:layout_height="@dimen/messaging_profile_pic_half_size"
        android:id="@+id/iv_top_right"
        android:scaleType="centerCrop"
        />
    <ImageView
        android:layout_width="@dimen/messaging_profile_pic_half_size"
        android:layout_height="@dimen/messaging_profile_pic_half_size"
        android:id="@+id/iv_bottom_right"
        android:paddingTop="@dimen/line_spacing"
        android:scaleType="centerCrop"
        />
</LinearLayout>

</nz.co.example.components.CircleLinearLayout>

我的循环linearlayout代码如下所示

public class CircleLinearLayout extends LinearLayout {

private Bitmap maskBitmap;
private Paint paint, maskPaint;
private float radius;

public CircleLinearLayout(Context context) {
    super(context);
    init(context, null, 0);
}

public CircleLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs, 0);
}

public CircleLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyle) {

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    if(attrs != null)
    {
        TypedArray a = context.getTheme().obtainStyledAttributes( attrs,R.styleable.CircleLinearLayout, defStyle , 0);

        try {
            radius = a.getDimension(R.styleable.CircleLinearLayout_diameter, getResources().getDimension(R.dimen.messaging_profile_pic_size)) / 2;
        } finally {
            a.recycle();
        }
    }

    setWillNotDraw(false);
}

@Override
public void draw(Canvas canvas) {
    Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas offscreenCanvas = new Canvas(offscreenBitmap);

    super.draw(offscreenCanvas);

    if (maskBitmap == null) {
        maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
    }

    offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
    canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
}

private Bitmap createMask(int width, int height) {
    Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
    Canvas canvas = new Canvas(mask);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);

    canvas.drawRect(0, 0, width, height, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    canvas.drawCircle(radius , radius , radius , paint);

    return mask;
   }
 }

干杯,Sree

 类似资料:
  • 我的应用程序中有一个椅子的ImageView对象。我有一个库,我想使用,并需要图像是BGR888图像格式。 null

  • 我正在寻找帮助开发(或一个库),可以让我合并在一起的多个图像到一个ImageView。 我的应用程序将用户之间的交互组合在一起,而不是单独地显示它们,因此我希望合并它们的所有化身,这样一个适配器单元格就可视化了一个“组”。 Facebook.com的聊天就是一个很好的例子: 我的问题是,我如何在Android/Java中提供这个功能?据推测,它的图像数量可能介于1到4之间。请告诉我您能给出的任何建

  • 问题内容: 我知道这应该很简单,但不会裁剪图像 我得到的图像宽为 1950像素 ,需要通过父对象的宽度进行裁剪。但是不会裁剪图像。我需要在布局中做什么才能仅显示前 400个像素 ,例如,无论屏幕/父级宽度是多少 对不起,一个简单的问题-尝试使用Google-仅存在复杂的问题。我是新手,所以请不要投票) 如果唯一的方法是以编程方式对其进行裁剪-请通过一种方法给我一个建议 问题答案: 好吧,我将评论粘

  • ImageView 和 TextView 一样是直接继承自 View 的基础控件,顾名思义,TextView 用来展示文本,那 ImageView 就是用来展示图片的了。因为 Android 碎片化严重,在 Android 中图片很容易使用但是却很难控制。不同的机型有不同的屏幕尺寸,导致对图片的适配要求很高。但是 Android 系统为我们提供了强大的图片控件,学好 ImageView 是做好 A

  • 这是另一个: 另外,我在其他片段中使uri公共静态化,以便在MainActivity中可以访问,下面是将捕获的图像加载到ImageView的代码:

  • 问题内容: 我正在尝试创建一个Rotatable ImageView,我将向其指定特定角度和枢轴点,并查看其围绕该枢轴点旋转。我尝试过这样的事情: 但是postRotate方法的参数(第二个和第三个-枢轴点)完全没有变化。即使它们是0、0,也一样。 所以我想创建一个ImageView,在初始化时将旋转一定角度。在此示例中为45度。我试图设置范围和人员..没有帮助。 我怎么做?:/ 问题答案: 您可