Android-只有2個圓角的ImageView,android-imageview

來源:互聯網
上載者:User

Android-只有2個圓角的ImageView,android-imageview

本例子是針對將圖片兩個直角轉換為圓角的例子:



1.自訂3個屬性 寬高,圓角值:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyPhotoView">        <attr name="width" format="dimension"></attr>        <attr name="height" format="dimension"></attr>        <attr name="radus" format="dimension"></attr>    </declare-styleable></resources>

2.在引用的布局中使用

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:bc="http://schemas.android.com/apk/res/org.lean"    android:layout_width="match_parent"    android:layout_height="match_parent"  ><org.lean.MyPhotoView    android:id="@+id/photo_iv"     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginTop="20dp"    android:layout_marginLeft="60dp"    bc:width="160dp"    bc:height="240dp"    bc:radus="10dp" /></RelativeLayout>

3.在Java代碼中設定兩個圖層。這裡有2個圖片 先繪製的叫靶心圖表層,然後設定混合渲染繪製 ,後繪製的圖層成為源圖層。

SRC_IN 模式在兩者相交的地方繪製源圖層(同時隱藏靶心圖表層),並且繪製的效果會受到靶心圖表像對應地方透明度的影響;


package org.lean;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.BitmapDrawable;import android.util.AttributeSet;import android.widget.ImageView;/** *重繪的ImageView * * @author Lean */public class MyPhotoView extends ImageView{private Bitmap mImageBtp;private Rect mImageSrcRect ,mImageDesRect;private Rect mBtmRect;private float mTotalWidth;private float mTotalHeight;private float mRadus;public MyPhotoView(Context context,int with,int height,float radus) {super(context);mTotalWidth=with;mTotalHeight=height;mRadus=radus;}public MyPhotoView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyPhotoView);mTotalWidth = array.getDimensionPixelSize(R.styleable.MyPhotoView_width, 0);mTotalHeight = array.getDimensionPixelSize(R.styleable.MyPhotoView_height, 0);mRadus = array.getDimensionPixelSize(R.styleable.MyPhotoView_radus, 0);array.recycle();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mImageDesRect=new Rect(0, 0,(int)mTotalWidth, (int)mTotalHeight);mBtmRect=new Rect(0, 0,(int)mTotalWidth,(int)(mTotalHeight+mRadus));}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);canvas.drawColor(Color.WHITE);int saveCount=canvas.saveLayer(0,0, mTotalWidth,mTotalHeight,paint,Canvas.ALL_SAVE_FLAG);if (mImageBtp!=null) {paint.setColor(Color.WHITE);canvas.drawRoundRect(new RectF(mBtmRect), mRadus, mRadus, paint);paint.setXfermode(xfermode);canvas.drawBitmap(mImageBtp,mImageSrcRect,mImageDesRect,paint);paint.setXfermode(null);}canvas.restoreToCount(saveCount);}@Overridepublic void setImageBitmap(Bitmap bm) {super.setImageBitmap(bm);mImageBtp=bm;reCalculateRect();}private void reCalculateRect() {mImageSrcRect=new Rect(0, 0,mImageBtp.getWidth(),mImageBtp.getHeight());invalidate();}@Overridepublic void setImageResource(int resId) {super.setImageResource(resId);mImageBtp=((BitmapDrawable)getResources().getDrawable(resId)).getBitmap();reCalculateRect();}}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.