Android代码使imageView轮不起作用

时间:2017-02-07 06:13:47

标签: android xml image android-layout imageview

我正在尝试进行ImageView轮。我编写了以下代码,使其显示为圆形,但不知怎的,它仍然显示为方ImageView。 [使用毕加索来获取图像]

Java代码:

ImageView iv = (ImageView) addLinkDialog.findViewById(R.id.group_icon_jsoup);
Picasso.with(getBaseContext()).load(GroupImageUrl).into(iv);
iv.setBackgroundResource(R.drawable.icon_img);

ImageView代码:

<ImageView
    android:id="@+id/group_icon_jsoup"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:background="@drawable/icon_img" />

@绘制/ icon_img.xml:

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

@绘制/ circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9"
    android:useLevel="false" >
<solid android:color="@android:color/transparent" />

<stroke
    android:width="10dp"
    android:color="@android:color/white" />
</shape>

6 个答案:

答案 0 :(得分:5)

为什么不使用第三方?

试试此代码

Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.add_image);

ImageView imageView = (ImageView) findViewById(R.id.imgProfilePicture);
imageView.setImageBitmap(getRoundedBitmap(picture));

 public Bitmap getRoundedBitmap(Bitmap bitmap){
        Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setAntiAlias(true);
        Canvas c = new Canvas(circleBitmap);
        c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
        return circleBitmap;
    }

您的xml文件

  <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imgProfilePicture"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginBottom="20dp"
        app:civ_border_width="3dp"
        app:civ_border_color="@color/light_gray" />

并在 build.gradle

中添加此内容
 compile 'de.hdodenhof:circleimageview:2.1.0'

Cirular ImageView完成!

enter image description here

答案 1 :(得分:3)

您是否只想使用代码,或者您也可以使用库?如果您对图书馆没问题,我建议使用this库,对我帮助很大。如果您不想使用库,可以使用RoundedBitmapDrawable:

RoundedBitmapDrawable drawable = 
       RoundedBitmapDrawableFactory.create(context.getResources(), bitmap)

drawable.setCircular(true);

在ImageView中使用此drawable。

答案 2 :(得分:1)

当您使用Picasso再次设置图像设置为imageView视图边界而不是您创建的背景时,会出现主要问题。

如果您以编程方式设置一个它将覆盖您的背景!

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</selector>

您可以将其设置为视图的背景。然后尝试使用view.setBackgroundResource(R.drawable.icon_img);。你会注意到这个变化!

您可以浏览Add a background image to shape in xml Android

Mask ImageView with round corner background

检查人们在这里尝试的各种方式!

但是毕加索可以直接与其他第三方合作。

  final ImageView imageView = (ImageView) findViewById(R.id.group_icon_jsoup);
    Picasso.with(YourActivity.this).load("http://i.imgur.com/DvpvklR.png")
            .resize(100, 100)
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() {
                    Bitmap imageBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                    RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
                    imageDrawable.setCircular(true);
                    imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
                    imageView.setImageDrawable(imageDrawable);
                }
                @Override
                public void onError() {
                    imageView.setImageResource(R.drawable.amanda);
                }
            });

答案 3 :(得分:1)

你好@Surjan这里的代码有助于创建任何形状的图像你只想要你需要的图像选择形状透明和任何其他颜色的组合,以下是例子:

protected Bitmap getPinnedImage(Bitmap original, int shapeImage) {
        if (original == null) {
            original = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_round_shape);
        }
        Bitmap mask = BitmapFactory.decodeResource(context.getResources(), shapeImage);

        original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);

        Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas mCanvas = new Canvas(result);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mCanvas.drawBitmap(original, 0, 0, null);
        mCanvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);
        return result;
    }

这是牵引参数,第一个是您的原始位图,第二个是您的形状可绘制,如下面是针形状

enter image description here 现在通过这个drawable你可以得到你的图像pin形状,无需访问任何第三方库。

答案 4 :(得分:0)

希望这能帮到你:

圆形按钮

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
    <shape android:shape="oval">
        <solid android:color="@color/colorPrimary"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="#c20586"/>
    </shape>
</item>
</selector>

在ImageView中使用它

<ImageView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:padding="10dp"
    android:src="@drawable/your_Image"
    android:background="@drawable/round_button"
    />

答案 5 :(得分:0)

试试这个,

ImageView iv = (ImageView) addLinkDialog.findViewById(R.id.group_icon_jsoup);

Picasso.with(getBaseContext()).load(GroupImageUrl).transform(new RoundedTransformation(5,15, Color.parseColor("#27a3cb"))).fit().into(iv);



public class RoundedTransformation implements Transformation {


private int mBorderSize;
private int mCornerRadius = 0;
private int mColor;

public RoundedTransformation(int borderSize, int color) {
    this.mBorderSize = borderSize;
    this.mColor = color;
}

public RoundedTransformation(int borderSize, int cornerRadius, int color) {
    this.mBorderSize = borderSize;
    this.mCornerRadius = cornerRadius;
    this.mColor = color;
}

@Override
public Bitmap transform(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap image = Bitmap.createBitmap(width, height, source.getConfig());
    Canvas canvas = new Canvas(image);
    canvas.drawARGB(0, 0, 0, 0);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Rect rect = new Rect(0, 0, width, height);


    if(this.mCornerRadius == 0) {
        canvas.drawRect(rect, paint);
    }
    else {
        canvas.drawRoundRect(new RectF(rect),this.mCornerRadius, this.mCornerRadius, paint);
    }

    paint.setXfermode(new PorterDuffXfermode((PorterDuff.Mode.SRC_IN)));
    canvas.drawBitmap(source, rect, rect, paint);

    Bitmap output;

    if(this.mBorderSize == 0) {
        output = image;
    }
    else {
        width = width + this.mBorderSize * 2;
        height = height + this.mBorderSize * 2;

        output = Bitmap.createBitmap(width, height, source.getConfig());
        canvas.setBitmap(output);
        canvas.drawARGB(0, 0, 0, 0);

        rect = new Rect(0, 0, width, height);

        paint.setXfermode(null);
        paint.setColor(this.mColor);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawRoundRect(new RectF(rect), this.mCornerRadius, this.mCornerRadius, paint);

        canvas.drawBitmap(image, this.mBorderSize, this.mBorderSize, null);
    }

    if(source != output) source.recycle();

    return output;
}

@Override
public String key() {
    return "bitmapBorder(" +
            "borderSize=" + this.mBorderSize + ", " +
            "cornerRadius=" + this.mCornerRadius + ", " +
            "color=" + this.mColor +")";
}

}