First, let's look at the effect chart.
Realize the idea
This is the two before and after the picture, the right second picture of the robbed label pictures when there is no product will show, in the center of each picture, the first idea is to imageview in the outer layer of relativelayout
Implementation methods
<relativelayout
android:layout_width= "match_parent"
android:layout_height= "wrap_content" >
<selectableroundedimageview
android:id= "@+id/imageview"
style= "@style/margin_distance"
android: Layout_width= "Match_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
android: background= "@drawable/youxuan_bg_shape_normol"
android:contentdescription= "@string/app_name"
Android :p adding= "1DP"
android:scaletype= "Centercrop"/>
<imageview android:id= "
@+id/iv_empty_pic"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_ Centerinparent= "true"/>
</RelativeLayout>
Of course it's OK, but if the XML layout itself is complicated, and it adds a layer to it, it's View Tree
not elegant enough, here's another way to do this: customizing view
public class CenterImage extends ImageView {private Paint Paint;
Private Boolean iscenterimgshow;
Private Bitmap Bitmap;
public void Setcenterimgshow (Boolean centerimgshow) {iscenterimgshow = Centerimgshow;
if (iscenterimgshow) {bitmap = Bitmapfactory.decoderesource (Getresources (), r.mipmap.ic_launcher);
Invalidate ();
} public CenterImage {super (context);
Init ();
Public CenterImage (context, AttributeSet attrs) {Super (context, attrs);
Init ();
Public CenterImage (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
Init ();
private void init () {paint = new paint ();
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); if (iscenterimgshow && bitmap!= null) {Canvas.drawbitmap (bitmap, Getmeasuredwidth ()/2-bitmap.getwidth ()/2, Getmeasuredheight ()/2-Bitmap.getheight ()/2, paint); }
}
}
In XML:
<com.henanjianye.soon.communityo2o.view.centerimage
android:id= "@+id/goodsimage"
android:layout_ Width= "Match_parent"
android:layout_height= "100DP"
android:layout_alignparentend= "true"
Android: Layout_alignparentright= "true"
android:contentdescription= "@string/app_name"
android:scaletype= " Centercrop "
android:src=" @mipmap/yijia_default_bg "/>
The object that gets the centerimage in the code:
CenterImage mgoodsimg = (centerimage) Findviewbyid (r.id.goodsimage);
Mgoodsimg.setcenterimgshow (TRUE);
When setCenterImgShow()
the invalidate()
method is called, the CenterImage method is onDraw()
called and redrawn again, so that you can happily add a new layer to the ImageView.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.