自定義View-第三步:Paint的方法與BitmapShader

挑選幾個(gè)Paint的方法講解一下

一、設(shè)置線冒(無(wú)、方、圓)->paint.setStrokeCap

一般進(jìn)度條尾部是圓的,就可以這樣子設(shè)置啦!引用自http://blog.csdn.net/harvic880925/article/details/51010839

二、設(shè)置外拐角(尖、直、圓)-> setStrokeJoin(Paint.Join join)

Paint paint = new Paint();  
paint.setStrokeWidth(40);  
paint.setColor(Color.GREEN);  
paint.setStyle(Paint.Style.STROKE);  
paint.setAntiAlias(true);  
  
Path path  = new Path();  
path.moveTo(100,100);  
path.lineTo(450,100);  
path.lineTo(100,300);  
paint.setStrokeJoin(Paint.Join.MITER);  
canvas.drawPath(path,paint);  
  
path.moveTo(100,400);  
path.lineTo(450,400);  
path.lineTo(100,600);  
paint.setStrokeJoin(Paint.Join.BEVEL);  
canvas.drawPath(path,paint);  
  
path.moveTo(100,700);  
path.lineTo(450,700);  
path.lineTo(100,900);  
paint.setStrokeJoin(Paint.Join.ROUND);  
canvas.drawPath(path,paint);  
引用自http://blog.csdn.net/harvic880925/article/details/51010839

可是內(nèi)拐角呢?怎么辦?別擔(dān)心,繼續(xù)看下去O(∩_∩)O~

三、更強(qiáng)大點(diǎn)的效果->setPathEffect(PathEffect effect)

1. CornerPathEffect——圓形拐角效果,有了CornerPathEffect,內(nèi)外拐角都不用擔(dān)心啦,可喜可賀!

//將原來(lái)Path生硬的直線拐角,變成圓形拐角:
public CornerPathEffect(float radius)  
  • radius:即當(dāng)前連接兩條直線所使用的圓的半徑

舉個(gè)栗子:

        paint.setColor(Color.BLACK);
        Path path = new Path();
        path.moveTo(100,500);
        path.lineTo(200,100);
        path.lineTo(400,500);
        canvas.drawPath(path,paint);

        paint.setColor(Color.RED);
        paint.setPathEffect(new CornerPathEffect(100));
        canvas.drawPath(path,paint);

        paint.setColor(Color.GREEN);
        paint.setPathEffect(new CornerPathEffect(200));
        canvas.drawPath(path,paint);
CornerPathEffect效果圖

2. DashPathEffect->虛線效果 :實(shí)現(xiàn)虛線段效果

//paint設(shè)置為STROKE 和 FILL_AND_STROKE有效
public DashPathEffect(float intervals[], float phase)  
  • intervals[]:表示組成虛線的各個(gè)線段的長(zhǎng)度;整條虛線就是由intervals[]中這些基本線段循環(huán)組成的.長(zhǎng)度必須大于等于2;因?yàn)楸仨氂幸粋€(gè)實(shí)線段和一個(gè)空線段來(lái)組成虛線。


個(gè)數(shù)必須為偶數(shù),如果是基數(shù),最后一個(gè)數(shù)字將被忽略;這個(gè)很好理解,因?yàn)橐唤M虛線的組成必然是一個(gè)實(shí)線和一個(gè)空線成對(duì)組成的。

  • phase:開(kāi)始繪制的偏移值 ,phase is an offset into the intervals array (mod the sum of all of the intervals). The intervals array controls the length of the dashes(囧囧翻譯:phase是相對(duì)于 intervals[]數(shù)組中所有數(shù)據(jù)的和的偏移量, intervals[]數(shù)組控制破折號(hào)的長(zhǎng)度)。
       Path path = new Path();
        path.moveTo(100, 300);
        path.lineTo(300, 100);
        path.lineTo(400, 700);

        canvas.drawPath(path, paint);
        paint.setColor(Color.RED);

//使用DashPathEffect畫(huà)線段
        paint.setPathEffect(new DashPathEffect(new float[]{20, 10, 10, 5}, 0));
        canvas.translate(10, 0);
        canvas.drawPath(path, paint);

//畫(huà)同一條線段,偏移值為15
        paint.setPathEffect(new DashPathEffect(new float[]{20, 10, 10, 5}, 15));
        paint.setColor(Color.BLUE);
        canvas.translate(10, 0);
        canvas.drawPath(path, paint);
DashPathEffect

利用這一特性,不斷改變偏移值,就可以形成一個(gè)動(dòng)畫(huà),

小車出發(fā),滴滴滴

代碼在這里:

       Path path = new Path();
        path.moveTo(0, 100);
        path.lineTo(getWidth(), 100);

        paint.setColor(Color.BLUE);
        canvas.drawPoint(200, 10, paint);
        //默認(rèn)43
        paint.setPathEffect(new DashPathEffect(new float[]{25, 3, 5, 10}, phase));
        canvas.drawPath(path, paint);
        try {
            Thread.sleep(5);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        phase--;
        if (phase <= 0) phase = 43;
        postInvalidate();

3. DiscretePathEffect—離散路徑效果:將原來(lái)路徑分隔成定長(zhǎng)的線段,然后將每條線段隨機(jī)偏移一段位置,我們可以用它來(lái)模擬一種類似生銹鐵絲的效果

public DiscretePathEffect(float segmentLength, float deviation)  
  • segmentLength:將原來(lái)的路徑切成多長(zhǎng)的線段。這個(gè)值越小,所切成的小線段越多;這個(gè)值越大,所切成的小線段越少。
  • deviation:表示被切成的每個(gè)小線段的可偏移距離。值越大,就表示每個(gè)線段的可偏移距離就越大,就顯得越凌亂,值越小,每個(gè)線段的可偏移原位置的距離就越小
    調(diào)用方式:
paint.setPathEffect(new DiscretePathEffect(6,5));  
    canvas.translate(0, 200);
        Random random1 = new Random(1);
        for (int i = 0; i < getWidth(); i += 40) {
            double height = random1.nextDouble();
            if (i == 0) path.moveTo(i, (float) (100 * height));
            else path.lineTo(i, (float) (100 * height));
        }
        //原圖
        paint.setColor(Color.RED);
        canvas.drawPath(path, paint);

        DiscretePathEffect discretePathEffect = new DiscretePathEffect(10, 8);
        canvas.translate(0, 30);
        paint.setColor(Color.GREEN);
        paint.setPathEffect(discretePathEffect);
        canvas.drawPath(path, paint);

        DiscretePathEffect discretePathEffect2 = new DiscretePathEffect(20, 8);
        canvas.translate(0, 30);
        paint.setColor(Color.GRAY);
        paint.setPathEffect(discretePathEffect2);
        canvas.drawPath(path, paint);

        DiscretePathEffect discretePathEffect3 = new DiscretePathEffect(10, 18);
        canvas.translate(0, 30);
        paint.setColor(Color.BLUE);
        paint.setPathEffect(discretePathEffect3);
        canvas.drawPath(path, paint);
效果對(duì)比圖

4. PathDashPathEffect—相當(dāng)于Photoshop中的印章功能,用另一個(gè)路徑圖案做為印章,沿著指定路徑一個(gè)個(gè)繪制上去.

public PathDashPathEffect(Path shape, float advance, float phase,Style style)   
  • Path shape:表示印章路徑,比如我們下面示例中的三角形加右上角一個(gè)點(diǎn)
  • float advance:表示兩個(gè)印章路徑間的距離,很容易理解,印章間距離越大,間距就越大。
  • float phase:路徑繪制偏移距離,與上面DashPathEffect中的float phase參數(shù)意義相同
  • Style style:表示在遇到轉(zhuǎn)角時(shí),如何操作印章以使轉(zhuǎn)角平滑過(guò)渡,取值有:Style.ROTATE,Style.MORPH,Style.TRANSLATE;Style.ROTATE表示通過(guò)旋轉(zhuǎn)印章來(lái)過(guò)渡轉(zhuǎn)角;Style.MORPH表示通過(guò)變形印章來(lái)過(guò)渡轉(zhuǎn)角;Style.TRANSLATE表示通過(guò)位移來(lái)過(guò)渡轉(zhuǎn)角

5.**ComposePathEffect與SumPathEffect **

public ComposePathEffect(PathEffect outerpe, PathEffect innerpe)  
public SumPathEffect(PathEffect first, PathEffect second)  
  • 利用ComposePathEffect先應(yīng)用innerpe特效,再應(yīng)用outerpe特效
  • 利用SumPathEffect,分別將first和second特效應(yīng)用于原始路徑,然后將生成的兩條特效路徑合并為新的特效

來(lái)個(gè)栗子吧

     canvas.translate(0, 200);
        Random random1 = new Random(1);
        for (int i = 0; i < getWidth(); i += 40) {
            double height = random1.nextDouble();
            if (i == 0) path.moveTo(i, (float) (100 * height));
            else path.lineTo(i, (float) (100 * height));
        }
        //原圖
        paint.setColor(Color.RED);
        canvas.drawPath(path, paint);

        //cornerPathEffect效果
        CornerPathEffect cornerPathEffect = new CornerPathEffect(100);
        canvas.translate(0, 50);
        paint.setColor(Color.GREEN);
        paint.setPathEffect(cornerPathEffect);
        canvas.drawPath(path, paint);

        //DiscretePathEffect效果
        DiscretePathEffect discretePathEffect = new DiscretePathEffect(10, 8);
        canvas.translate(0, 30);
        paint.setColor(Color.BLUE);
        paint.setPathEffect(discretePathEffect);
        canvas.drawPath(path, paint);

       //ComposePathEffect效果
        canvas.translate(0, 50);
        paint.setColor(Color.LTGRAY);
        paint.setPathEffect(new ComposePathEffect(cornerPathEffect, discretePathEffect));
        canvas.drawPath(path, paint);

        //SumPathEffect
        canvas.translate(0, 50);
        paint.setColor(Color.GRAY);
        paint.setPathEffect(new SumPathEffect(cornerPathEffect, discretePathEffect));
        canvas.drawPath(path, paint);    
效果圖

四、Paint的setShader(Shader shader)

 它有五個(gè)子類,像PathEffect一樣,它的每個(gè)子類都實(shí)現(xiàn)了一種Shader。

1. BitmapShader:用來(lái)實(shí)現(xiàn)圓形圖片的利器

BitmapShader (Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)
  • bitmap: 要處理的bitmap對(duì)象
  • tileX(tileY):在X(Y)軸處理的效果,Shader.TileMode 模式有三種:CLAMP、MIRROR和REPETA

各個(gè)模式介紹

模式 效果
Shader.TileMode.CLAMP 會(huì)將邊緣的一個(gè)像素進(jìn)行拉伸、擴(kuò)展
Shader.TileMode.MIRROR 鏡面翻轉(zhuǎn)
Shader.TileMode.REPEAT 平移復(fù)制

CLAMP:邊緣像素?cái)U(kuò)展拉伸

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bears);

        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        //原圖
        canvas.drawBitmap(bitmap, 0, 0, paint);
        //右圖
        canvas.drawRect(0, bitmap.getHeight() + 50, bitmap.getWidth(), bitmap.getHeight() * 2 + 50, paint);
        //下圖
        canvas.drawRect(bitmap.getWidth() + 50, 0, bitmap.getWidth() * 2, bitmap.getHeight(), paint);
        //右下
        canvas.drawRect(bitmap.getWidth() + 50, bitmap.getHeight() + 50, bitmap.getWidth()*2, bitmap.getHeight() * 2 + 50, paint);

CLAMP效果圖

MIRROR:鏡面翻轉(zhuǎn)

//只改這一句
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR));

MIRROR

MIRROR和CLAMP結(jié)合

 paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.MIRROR));
X:CLAMP;Y:MIRROR
  paint.setShader(new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.CLAMP));

X:MIRROR;Y:CLAMP

由此可知:
** 繪制過(guò)程是先采用Y軸模式,再使用X軸模式的。**

REPEAT:平移復(fù)制

REPEAT效果圖

畫(huà)個(gè)圓試試效果

      paint.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        //原圖
        canvas.drawBitmap(bitmap, 0, 0, paint);
        //右圖
        canvas.drawCircle(bitmap.getWidth()+100,bitmap.getHeight()/2,50, paint);
        //下圖
        canvas.drawCircle(bitmap.getWidth(), bitmap.getHeight()+300,250,paint);
效果圖

圓角圖片閃亮登場(chǎng)
前景提要:

  1. 有一個(gè) Android BitmapShader 實(shí)戰(zhàn) 實(shí)現(xiàn)圓形、圓角圖片的博客,大家可以看一下如何實(shí)現(xiàn)圓角圖片,下邊也會(huì)把代碼放上去的,改了一點(diǎn)點(diǎn)。另外,github中的CircleImageView也是基于BitmapShader實(shí)現(xiàn)的,只是這個(gè)沒(méi)有博客那么詳細(xì)的介紹。
  2. 代碼中用了封裝控件,如果不懂的話,可以先看封裝控件這一章節(jié)內(nèi)容.
  3. 另外,補(bǔ)充一下 Android BitmapShader 實(shí)戰(zhàn) 實(shí)現(xiàn)圓形、圓角圖片中用到的一個(gè)知識(shí)點(diǎn)TypedValue.applyDimension

Resources resources = getResources();
float fPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, resources.getDisplayMetrics());

第一個(gè)參數(shù)為 單位,第二個(gè)參數(shù)為單位(第一個(gè)參數(shù)設(shè)置的單位)指定的值,返回值 都是像素,如TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 300, resources.getDisplayMetrics()); 就是 像素 轉(zhuǎn) 像素,還是300.

####放代碼~O(∩_∩)O~
先來(lái)效果圖:
![效果圖](http://upload-images.jianshu.io/upload_images/3679520-ba49001ab565d0fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
這是原圖片:
![原圖](http://upload-images.jianshu.io/upload_images/3679520-e9d4c1a6624c6d9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/120)
實(shí)現(xiàn)的代碼:
1. attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MineView">
<attr name="borderRadius" format="dimension"/>
<attr name="type">
<enum name="circle" value="0"/>
<enum name="round" value="1" />
</attr>
</declare-styleable>
</resources>

2. 自定義ImageView

public class MineView extends ImageView {
/**
* 圖片的類型,圓形or圓角
/
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;
private int borderRadius, mRadius;// 圓角或圓形半徑
private int mWidth; //圓的時(shí)候,設(shè)置view的大小
private Matrix matrix;
private Paint paint;
private RectF rectF;
private BitmapShader bitmapShader;
/
*
* 圓角大小的默認(rèn)值
*/
private static final int BODER_RADIUS_DEFAULT = 10;

public MineView(Context context, AttributeSet attrs) {
    super(context, attrs);

//獲取設(shè)置的數(shù)值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MineView);
borderRadius = typedArray.getDimensionPixelSize(R.styleable.MineView_borderRadius,dp2px(BODER_RADIUS_DEFAULT));
type = typedArray.getInt(R.styleable.MineView_type, TYPE_CIRCLE);
typedArray.recycle();
//初始化
matrix = new Matrix();
paint = new Paint();
paint.setAntiAlias(true);
}

public MineView(Context context) {
    super(context, null);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    /**
     * 如果類型是圓形,則強(qiáng)制改變view的寬高一致,以小值為準(zhǔn)
     */
    if (type == TYPE_CIRCLE) {
        mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = mWidth / 2;
        //設(shè)置類型為圓形時(shí),讓view寬高一致
        setMeasuredDimension(mWidth, mWidth);
    }
}

/**
 * 初始化BitmapShader
 */
private void setUpShader() {
    Drawable drawable = getDrawable();
    if (drawable == null) return;
    Bitmap bitmap = drawableToBitmap(drawable);

    //將bmp作為著色器,就是在指定區(qū)域內(nèi)繪制bmp
    bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    float scale = 1.0f;
    if (type == TYPE_CIRCLE) {
        scale = Math.max(mWidth * 1.0f / bitmap.getWidth(), mWidth * 1.0f / bitmap.getHeight());

// int bitmapSize=Math.min(bitmap.getWidth(),bitmap.getHeight());
// scale=mWidth1.0f/bitmapSize;
}
else if (type == TYPE_ROUND) {
//比如:view的寬高為10
20;圖片的寬高為5*100 ; 最終我們應(yīng)該按照寬的比例放大,而不是按照高的比例縮??;因?yàn)槲覀冃枰尶s放后的圖片,自定大于我們的view寬高,并保證原圖比例。相當(dāng)于centerCrop
scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
}
matrix.setScale(scale, scale);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
}

@Override
protected void onDraw(Canvas canvas) {
    //TODO 不要調(diào)用super.onDraw!!!
   // super.onDraw(canvas);

    if (getDrawable() == null) return;
    setUpShader();
    if (type == TYPE_ROUND) {
        canvas.drawRoundRect(rectF, borderRadius, borderRadius, paint);
    }
    else
        canvas.drawCircle(mRadius, mRadius, mRadius, paint);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (type == TYPE_ROUND) {
        // 圓角圖片的范圍
        rectF = new RectF(0, 0, getWidth(), getHeight());
    }
}

/**
 * 修改圓角大小
 */
public void setBorderRadius(int borderRadius) {
    int pxVal = dp2px(borderRadius);
    if (this.borderRadius != pxVal) {
        this.borderRadius = pxVal;
        invalidate();
    }
}

/**
 * 修改type
 */
public void setType(int type) {
    if (this.type != type) {
        this.type = type;
        if (this.type != TYPE_ROUND && this.type != TYPE_CIRCLE) {
            this.type = TYPE_CIRCLE;
        }
        //從measure開(kāi)始,對(duì)于每一個(gè)含有標(biāo)記位的view及其子View都會(huì)進(jìn)行測(cè)量、布局、繪制
        requestLayout();
    }
}

public int dp2px(int dpVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            dpVal, getResources().getDisplayMetrics());
}

/**
 * drawable轉(zhuǎn)bitmap
 */
private Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        return bitmapDrawable.getBitmap();
    }
    try {
        Bitmap bitmap;
        /**
         * http://www.codexiu.cn/android/blog/14321/
         ColorDrawable、BitmapDrawable和ClipDrawable的區(qū)別
         */
        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        }
        else
            // drawable.getIntrinsicHeight()返回的是dp
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

}


3.xml布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.crossroads.demo.demo.MainActivity">

<com.crossroads.demo.demo.MineView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/heartflow"
app:borderRadius="30dp"
app:type="round"
/>

<com.crossroads.demo.demo.MineView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:src="@mipmap/heartflow"
app:borderRadius="30sp"
app:type="round"/>

<com.crossroads.demo.demo.MineView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/heartflow"
app:borderRadius="30px"
app:type="round"
/>

<com.crossroads.demo.demo.MineView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/heartflow"/>

<!--15-->
<com.crossroads.demo.demo.MineView
    android:layout_width="60dp"
    android:layout_height="80dp"
    android:src="@mipmap/heartflow"/>

</LinearLayout>


##### 后記
 其他的ComposeShader、[LinearGradient](http://www.itdecent.cn/p/c2b5d7fbf8ca)、[RadialGradient](http://www.itdecent.cn/p/b20c4a6a1b0f)、[SweepGradient](http://www.itdecent.cn/p/c2b5d7fbf8ca)將在之后詳細(xì)講解哦!
####補(bǔ)充->Paint的各種方法

1.圖形繪制
setARGB(int a,int r,int g,int b); // 設(shè)置繪制的顏色,a代表透明度,r,g,b代表顏色值。
setAlpha(int a); //設(shè)置繪制圖形的透明度。
setColor(int color); //設(shè)置繪制的顏色,使用顏色值來(lái)表示,該顏色值包括透明度和RGB顏色。
setAntiAlias(boolean aa); //設(shè)置是否使用抗鋸齒功能,會(huì)消耗較大資源,繪制圖形速度會(huì)變慢。
setDither(boolean dither); //設(shè)定是否使用圖像抖動(dòng)處理,會(huì)使繪制出來(lái)的圖片顏色更加平滑和飽滿,圖像更加清晰
setFilterBitmap(boolean filter); //如果該項(xiàng)設(shè)置為true,則圖像在動(dòng)畫(huà)進(jìn)行中會(huì)濾掉對(duì)Bitmap圖像的優(yōu)化操作,加快顯示 速度,本設(shè)置項(xiàng)依賴于dither和xfermode的設(shè)置
setMaskFilter(MaskFilter maskfilter); // 設(shè)置MaskFilter,可以用不同的MaskFilter實(shí)現(xiàn)濾鏡的效果,如濾化,立體等
setColorFilter(ColorFilter colorfilter); // 設(shè)置顏色過(guò)濾器,可以在繪制顏色時(shí)實(shí)現(xiàn)不用顏色的變換效果
setPathEffect(PathEffect effect); // 設(shè)置繪制路徑的效果,如點(diǎn)畫(huà)線等
setShader(Shader shader); //設(shè)置圖像效果,使用Shader可以繪制出各種漸變效果
setShadowLayer(float radius ,float dx,float dy,int color);//在圖形下面設(shè)置陰影層,產(chǎn)生陰影效果,radius為陰影的角度,dx和dy為陰影在x軸和y軸上的距離,color為陰影的顏色
setStyle(Paint.Style style); //設(shè)置畫(huà)筆的樣式,為FILL,F(xiàn)ILL_OR_STROKE,或STROKE
setStrokeCap(Paint.Cap cap); //當(dāng)畫(huà)筆樣式為STROKE或FILL_OR_STROKE時(shí),設(shè)置筆刷的圖形樣式,如圓形樣式 Cap.ROUND,或方形樣式Cap.SQUARE .
setSrokeJoin(Paint.Join join); // 設(shè)置繪制時(shí)各圖形的結(jié)合方式Join.MITER(結(jié)合處為銳角)Join.Round(結(jié)合處為圓弧) Join.BEVEL(結(jié)合處為直線)
setStrokeWidth(float width); // 當(dāng)畫(huà)筆樣式為STROKE或FILL_OR_STROKE時(shí),設(shè)置筆刷的粗細(xì)度
setXfermode(Xfermode xfermode); //設(shè)置圖形重疊時(shí)的處理方式,如合并,取交集或并集,經(jīng)常用來(lái)制作橡皮的擦除效果

2.文本繪制
setFakeBoldText(boolean fakeBoldText); // 模擬實(shí)現(xiàn)粗體文字,設(shè)置在小字體上效果會(huì)非常差
setSubpixelText(boolean subpixelText); //設(shè)置該項(xiàng)為true,將有助于文本在LCD屏幕上的顯示效果
setTextAlign(Paint.Align align); // 設(shè)置繪制文字的對(duì)齊方向
setTextScaleX(float scaleX); //設(shè)置繪制文字x軸的縮放比例,可以實(shí)現(xiàn)文字的拉伸的效果
setTextSize(float textSize); //設(shè)置繪制文字的字號(hào)大小
setTextSkewX(float skewX); // 設(shè)置斜體文字,skewX為傾斜弧度
setTypeface(Typeface typeface); // 設(shè)置Typeface對(duì)象,即字體風(fēng)格,包括粗體,斜體以及襯線體,非襯線體等
setUnderlineText(boolean underlineText); //設(shè)置帶有下劃線的文字效果
setStrikeThruText(boolean strikeThruText); //設(shè)置帶有刪除線的效果


####參考網(wǎng)址
[[Paint之函數(shù)大匯總](http://blog.csdn.net/harvic880925/article/details/51010839)](http://blog.csdn.net/harvic880925/article/details/51010839)
[paint的setShader](http://www.cnblogs.com/tianzhijiexian/p/4298660.html)
 [TypedValue.applyDimension,代碼實(shí)現(xiàn)px與dip轉(zhuǎn)換](http://blog.csdn.net/lzhang616/article/details/8945729)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 系列文章之 Android中自定義View(一)系列文章之 Android中自定義View(二)系列文章之 And...
    YoungerDev閱讀 2,328評(píng)論 0 4
  • Paint類 setAlpha(int a) 透明度 setColor(int color)設(shè)置顏色值 setAn...
    wdply閱讀 1,127評(píng)論 0 0
  • 一蓑煙雨, 落滿了花季。 一簾闕詞, 平仄了相思。 我愿用一支素筆, 畫(huà)一雙, 不再憂傷的眼眸。
    森林_木閱讀 544評(píng)論 0 0
  • 今天這個(gè)作業(yè)題目真心沒(méi)法寫(xiě),首先是我沒(méi)什么崇拜的人,其次我沒(méi)啥可采訪的,沒(méi)什么想問(wèn)的。但由這個(gè)題目想起了我...
    心無(wú)速閱讀 358評(píng)論 2 0

友情鏈接更多精彩內(nèi)容