简体   繁体   English

Android - 具有多个位图的Imageview

[英]Android - Imageview with multiple bitmaps

I am working on a chat application, which does need to show the profile picture of each person whom I'm chatting with. 我正在开发一个聊天应用程序,它需要显示我正在聊天的每个人的个人资料图片。 If it is a group conversation I need to design a layout like FB as below 如果是群组对话,我需要设计像FB这样的布局,如下所示

在此输入图像描述

I'm thinking of implementing it using LayerDrawable but not sure how. 我正在考虑使用LayerDrawable实现它,但不知道如何实现它。 Also the images need to be loaded from server . 还需要从server加载图像。 I'm using Glide library to load images . 我正在使用Glide库来加载images

Considered 3 layouts side by side and wrap the top layout into circular shape. 并排考虑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>

And my Circular linearlayout code goes like this 我的循环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;
   }
 }

Cheers, Sree 干杯,Sree

Yes, you would need to implement LayerDrawable . 是的,您需要实现LayerDrawable On StackOverflow there are some examples like this below from overlay two images in android to set an imageview issue 在StackOverflow上,有一些例子如下所示, 在android中覆盖两个图像来设置一个imageview问题

You can skip the complex Canvas manipulation and do this entirely with Drawables, using LayerDrawable . 您可以跳过复杂的Canvas操作,并使用LayerDrawable完全使用Drawables完成此操作。 You have one of two choices: You can either define it in XML then simply set the image, or you can configure a LayerDrawable dynamically in code. 您有两种选择之一:您可以在XML中定义它,然后只需设置图像,或者您可以在代码中动态配置LayerDrawable

Solution #1 (via XML): 解决方案#1(通过XML):

Create a new Drawable XML file, let's call it layer.xml : 创建一个新的Drawable XML文件,我们称之为layer.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/t" /> <item android:drawable="@drawable/tt" /> </layer-list> 

Now set the image using that Drawable: 现在使用Drawable设置图像:

 testimage.setImageDrawable(getResources().getDrawable(R.layout.layer)); 

Solution #2 (dynamic): 解决方案#2(动态):

 Resources r = getResources(); Drawable[] layers = new Drawable[2]; layers[0] = r.getDrawable(R.drawable.t); layers[1] = r.getDrawable(R.drawable.tt); LayerDrawable layerDrawable = new LayerDrawable(layers); testimage.setImageDrawable(layerDrawable); 

(I haven't tested this code so there may be a mistake, but this general outline should work.) (我没有测试过这段代码,所以可能会出现错误,但这个大纲应该有效。)

Another possible solution might be from unable to set multiple bitmap in one imageview 另一种可能的解决方案可能是无法在一个imageview中设置多个位图

you can do something like the following : 你可以做以下的事情:

 public Bitmap drawMultipleBitmapsOnImageView(Bitmap b) { Bitmap drawnBitmap = null; try { drawnBitmap = Bitmap.createBitmap(400, 400, Config.ARGB_8888); Canvas canvas = new Canvas(drawnBitmap); // JUST CHANGE TO DIFFERENT Bitmaps and coordinates . canvas.drawBitmap(b, 100, 100, null); canvas.drawBitmap(b, 200, 300, null); canvas.drawBitmap(b, 100, 200, null); canvas.drawBitmap(b, 300, 350, null); } catch (Exception e) { e.printStackTrace(); } return drawnBitmap; } 

you call this method like the following : 你可以像下面这样调用这个方法:

 ImageView myImageView = (ImageView) findViewById(R.id.myImageView); Bitmap bitmap = ((BitmapDrawable) myImageView.getDrawable()) .getBitmap(); Bitmap b = drawMultipleBitmapsOnImageView(bitmap); myImageView.setImageBitmap(b); 

If you still feel not satisfied, take a look at Android : How to Draw multiple images inside the same imageview but shifted in the y coordinate? 如果您仍然感到不满意,请查看Android:如何在同一个图像视图中绘制多个图像但是在y坐标中移动?

use this code for show 2 image in one image: 在一个图像中使用此代码显示show 2图像:

  ImageView myImageView = (ImageView) findViewById(R.id.img1); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.call); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.available); Bitmap combinedBitmap = getCombinedBitmap(pic2, pic1); myImageView.setImageBitmap(b); 

and this is the getCombinedBitmap() method : 这是getCombinedBitmap()方法:

 public Bitmap getCombinedBitmap(Bitmap b, Bitmap b2) { Bitmap drawnBitmap = null; try { drawnBitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888); Canvas canvas = new Canvas(drawnBitmap); // JUST CHANGE TO DIFFERENT Bitmaps and coordinates . canvas.drawBitmap(b, 0, 0, null); canvas.drawBitmap(b2, 0, 0, null); //for more images : // canvas.drawBitmap(b3, 0, 0, null); // canvas.drawBitmap(b4, 0, 0, null); } catch (Exception e) { e.printStackTrace(); } return drawnBitmap; } 

You can also visit: Draw multiple bitmaps on Imageview 您还可以访问: 在Imageview上绘制多个位图

On Web you would also find some tutorials, but I don't think that presented there solution would be much different from this above. 在Web上你也会找到一些教程,但我不认为那里提供的解决方案与上面的内容有很大的不同。


Finally, once you've done it to make it circle use this library: https://github.com/hdodenhof/CircleImageView 最后,一旦你完成它使圈子使用这个库: https//github.com/hdodenhof/CircleImageView

Hope it help 希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM