編輯:關於Android編程
Android圓形圖片和圓角圖片的繪制。
自定義ImageView
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.graphics.Bitmap.Config; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.View; import android.view.View.MeasureSpec; import com.aynu.circleimageview.R; /** * * @author zhy * */ public class CustomImageView extends View { /** * TYPE_CIRCLE / TYPE_ROUND */ private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; /** *bitmap */ private Bitmap mSrc; /** *半徑 */ private int mRadius; /** * ?ؼ?長度? */ private int mWidth; /** * ?高度? */ private int mHeight; public CustomImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomImageView(Context context) { this(context, null); } /** * ?獲取屬性值?? * * @param context * @param attrs * @param defStyle */ public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomImageView_src://獲取圖片,自定義屬性,設置圖片並獲取 mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0)); break; case R.styleable.CustomImageView_type://獲取圖片要設置圓形還是圓角圖片 type = a.getInt(attr, 0); break; case R.styleable.CustomImageView_borderRadius://半徑值 mRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics())); break; } } a.recycle(); } /** * ????ؼ??測量? */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); /** * ???獲取寬度測量模式和大小? */ int specMode = MeasureSpec.getMode(widthMeasureSpec); int specSize = MeasureSpec.getSize(widthMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate精確的 { mWidth = specSize; } else { // ? int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();//圖片的大小,和測量出來的大小比較 if (specMode == MeasureSpec.AT_MOST)// wrap_content { mWidth = Math.min(desireByImg, specSize); } } /*** * ??獲取高度的測量模式? */ specMode = MeasureSpec.getMode(heightMeasureSpec); specSize = MeasureSpec.getSize(heightMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate { mHeight = specSize; } else { int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { mHeight = Math.min(desire, specSize); } } setMeasuredDimension(mWidth, mHeight); } /** * ???? */ @Override protected void onDraw(Canvas canvas) { switch (type) { // ???圓形圖片 case TYPE_CIRCLE: int min = Math.min(mWidth, mHeight); /** * 使得當前的圖片的寬和高一致Bitmap */ mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false); //繪制圓角圖片 canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null); break; case TYPE_ROUND: //圓角圖片 canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null); break; } } /** * * @param source * @param min * @return */ private Bitmap createCircleImage(Bitmap source, int min) { //畫筆 final Paint paint = new Paint(); paint.setAntiAlias(true); //創建Bitmap對象 Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888); /** * 繪制? */ Canvas canvas = new Canvas(target); /** * 畫圓 */ canvas.drawCircle(min / 2, min / 2, min / 2, paint); /** * ???設置mode?????? */ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); /** * ???畫圖像 */ canvas.drawBitmap(source, 0, 0, paint); return target; } /** * ?? * * @param source * @return */ private Bitmap createRoundConerImage(Bitmap source) { final Paint paint = new Paint(); paint.setAntiAlias(true); //創建bitmap Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888); //繪制圖像 Canvas canvas = new Canvas(target); RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rect, 50f, 50f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //圖片,大小,繪制的地方 canvas.drawBitmap(source, 0, 0, paint); return target; } }
activity_main.xml文件:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:zhy="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@mipmap/icon"> <com.aynu.circleimageview.customimageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:src="@mipmap/icon" zhy:type="circle"> <com.aynu.circleimageview.customimageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:borderradius="10dp" zhy:src="@mipmap/icon" zhy:type="round"> <com.aynu.circleimageview.customimageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:borderradius="20dp" zhy:src="@mipmap/icon" zhy:type="round"> </com.aynu.circleimageview.customimageview></com.aynu.circleimageview.customimageview></com.aynu.circleimageview.customimageview></imageview></linearlayout>
res/attrs文件
<!--?xml version="1.0" encoding="utf-8"?--> <resources> <enum name="circle" value="0"> <enum name="round" value="1"> </enum></enum></attr> </attr> <declare-styleable name="CustomImageView"> </attr></attr></attr></declare-styleable> </attr></resources>
在網上查了好多資料,大致都雷同,大家都是互相抄襲的,看著很費勁,不好理解,自己總結一下,留著需要看的話來查找。代碼中的例子如下 <ImageView
前言目前注解的使用頻率還是挺高,像第三方butterknife、數據庫ActiveAndroid等等,通過注解,我們的開發效率得到了明顯提高。因此理解注解並熟練使用注解是
實現了浏覽器的返回 前進 主頁 退出 輸入網址的功能注釋的很清楚啦 就不多說了首先是布局文件 <linearlayout xmlns:android=&q
CoordinatorLayout 實現了多種Material Design中提到的滾動效果。目前這個框架提供了幾種不用寫動畫代碼就能工作的方法,這些效果包括:*讓浮動操