本文主要是介绍自定义控件(26)---图片伸缩、圆角切割、内存位图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图如下:::
需要的素材
可以了解:如何图片伸缩,以及图片的圆角切割(内存生成位图)
<RelativeLayout 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"tools:context="com.example.customeprogressbar.MainActivity" ><com.example.customeprogressbar.CustomProgressBarandroid:id="@+id/progress"android:layout_width="180dip"android:layout_height="10dip"android:layout_centerInParent="true"android:background="@drawable/pro_line" /></RelativeLayout>
CustomProgressBar
package com.example.customeprogressbar;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;public class CustomProgressBar extends View {int width;int height;Bitmap sourBg;Bitmap barBg;/*** 构造方法*/public CustomProgressBar(Context ctx) {super(ctx);}public CustomProgressBar(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);}public CustomProgressBar(Context context, AttributeSet attrs) {super(context, attrs);}/*** * android:layout_width="180dip" android:layout_height="10dip"*/@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {this.width = getWidth();this.height = getHeight();// 获取进度条矩形图片this.sourBg = BitmapFactory.decodeResource(getResources(),R.drawable.pro_state);// 裁剪跟背景图同样大小的矩形图片(非圆角)this.sourBg = conBitmapSize(this.sourBg, this.width, this.height);// 裁剪圆角图片this.barBg = toRoundCorner(this.sourBg, dip2px(getContext(), 4.33f));// 4.33f表示进度条的圆角大小super.onSizeChanged(w, h, oldw, oldh);}protected void onDraw(Canvas canvas) {if (this.barBg != null) {canvas.drawBitmap(this.barBg, 0.0F, 0.0F, null);}super.onDraw(canvas);}/*** 通过缩放比例那个背景图片,然后生成跟底部图片大小一样的(除去圆角) Bitmap.createBitmap* * @param bitmap* 进度的背景图片* @param width* 控件宽* @param height* 控件高*/private Bitmap conBitmapSize(Bitmap bitmap, int width, int height) {Matrix matrix = new Matrix();// 缩放比例matrix.setScale(1.0F * width / bitmap.getWidth(), 1.0F * height/ bitmap.getHeight());bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, false);return bitmap;}/**切割圆角:::::* 创建一个以位图为底的画布 在canvas初始化的时候就传入了一个空的bitmap* 最后canvas中绘画的内容都被绘制到了bitmap中,从而得到了我们需要的bitmap*/private Bitmap toRoundCorner(Bitmap bitmap, int pixels) {// 生成一个位图图像Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(output);Paint paint = new Paint();paint.setAntiAlias(true);Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());RectF rectF = new RectF(rect);float roundPx = pixels;/*** rect:RectF对象。* * rx:x方向上的圆角半径。* * ry:y方向上的圆角半径。* * paint:绘制时所使用的画笔。*/canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);
// canvas.drawBitmap(bitmap, src, dst, paint)return output;}public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}
}
这篇关于自定义控件(26)---图片伸缩、圆角切割、内存位图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!