Android custom ImageView To add layer effects to the picture _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.