GrI*_*sHu 32 android listview imageview android-layout
我有一个Custom ListView包含ImageView和TextView.一切都很好.
我想要的是列表中显示的图像是在圆角.从Webservice我得到矩形的图像.但我想在圆角显示ImageView如下.

任何人都可以告诉我如何在圆角掩盖图像?
我已经尝试过创建如下的drawable文件并将其作为src应用于ImageView.但没有什么对我有用.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval" >
<solid android:color="#FFFFFF" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="oval" >
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
编辑:
请帮我.
任何帮助将不胜感激.
谢谢
我申请了以下解决方案:
<FrameLayout
android:id="@+id/imagemaskframe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<ImageView
android:id="@+id/op_ivpic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/iv_mask_op"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/imgmask" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
sri*_*ani 32
最好的方法是Canvas使用PorterDuff操作和/或Shaders.假设您Bitmap可以使用并存储mBitmap.
选项1:使用着色器.
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Load the bitmap as a shader to the paint.
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
final Shader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Draw a circle with the required radius.
final float halfWidth = canvas.getWidth()/2;
final float halfHeight = canvas.getHeight()/2;
final float radius = Math.max(halfWidth, halfHeight);
canvas.drawCircle(halfWidth, halfHeight, radius, paint);
}
Run Code Online (Sandbox Code Playgroud)
选项2:使用PorterDuff模式.
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Create a circular path.
final float halfWidth = canvas.getWidth()/2;
final float halfHeight = canvas.getHeight()/2;
final float radius = Math.max(halfWidth, halfHeight);
final Path path = new Path();
path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawPath(path, paint);
}
Run Code Online (Sandbox Code Playgroud)
注意:
Canvas可能需要保存并在没有硬件纹理支持时恢复.这里没有提到围绕它的一般想法.setWillNotDraw(false);到构造函数中.其他参考文献:
Shaders.PathFirefox for Android中的弧形按钮.CanvasICS之前的保存,恢复和特殊情况的信息.小智 20
我肯定会像其他人一样推荐毕加索.我的一个Activity类的这段代码为我做了诀窍.它使用了我在color.xml中定义的颜色和简单的布局(如下所示).
ImageView profile_image = (ImageView) findViewById(R.id.profile_image);
mContext = profile_image.getContext();
// ----------------------------------------------------------------
// apply rounding to image
// see: https://github.com/vinc3m1/RoundedImageView
// ----------------------------------------------------------------
Transformation transformation = new RoundedTransformationBuilder()
.borderColor(getResources().getColor(R.color.my_special_orange))
.borderWidthDp(5)
.cornerRadiusDp(50)
.oval(false)
.build();
Picasso.with(mContext)
.load("http://{some_url}.jpg")
.fit()
.transform(transformation)
.into(profile_image);
Run Code Online (Sandbox Code Playgroud)
和相应的布局文件:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:padding="12dp">
<ImageView
android:id="@+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:padding="12dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First-Name"
android:id="@+id/profile_first_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Lastname"
android:id="@+id/profile_last_name" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是结果:
小智 17
我建议你使用另一种方法:
一个FrameLayout和两个ImageView可以做到这一点.
<FrameLayout>
<ImageView /> your image
<ImageView /> put a image which has a transparent circle in it
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
然后你的图像可以通过透明圆圈看到.
God*_*uke 15
甲RoundedBitmapDrawable从V4支持库可被应用于一个ImageView以实现期望的效果:
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap avatar = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
RoundedBitmapDrawable roundDrawable = RoundedBitmapDrawableFactory.create(getResources(), avatar);
roundDrawable.setCircular(true);
imageView.setImageDrawable(roundDrawable);
Run Code Online (Sandbox Code Playgroud)
一个简单的解决方案,没有黑色背景!
public class CircleImageView extends ImageView
{
public CircleImageView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
// Create a circular path.
final float halfWidth = canvas.getWidth()/2;
final float halfHeight = canvas.getHeight()/2;
final float radius = Math.max(halfWidth, halfHeight);
final Path path = new Path();
path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);
canvas.clipPath(path);
super.onDraw(canvas);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84689 次 |
| 最近记录: |