zoukankan      html  css  js  c++  java
  • BitmapShader填充图形

    package com.loaderman.customviewdemo;
    
    import android.content.Context;
    import android.graphics.*;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class BitmapShaderView extends View {
        private Paint mPaint;
        private Bitmap mBmp;
    
        public BitmapShaderView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPaint = new Paint();
            mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.dog_edge);
            /*
            *       CLAMP  用边缘色彩来填充多余的空间
            *       MIRROR 重复使用镜像模式的图像来填充多余的空间
            *      REPEAT 重复原图像来填充多余的空间
            */
            mPaint.setShader(new BitmapShader(mBmp, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.WHITE);
            //getWidth()用于获取控件宽度,getHeight()用于获取控件高度
            float left = getWidth() / 3;
            float top = getHeight() / 3;
            float right = getWidth() * 2 / 3;
            float bottom = getHeight() * 2 / 3;
    
            canvas.drawRect(left, top, right, bottom, mPaint);
    //        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@android:color/white"
        >
     <com.loaderman.customviewdemo.BitmapShaderView
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    </LinearLayout>

    效果:

    说明:

    BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY) 

    bitmap指定图案

    tileX指定当X轴超出单张图片大小时所重复的策略

    tileY指定当Y轴超出单张图片大小时所使用的重复策略

  • 相关阅读:
    python ascii codec can't decode
    python文件编码说明 coding=utf-8
    windbg的使用
    在后台运行Python脚本服务
    ubuntu下更改分辨率
    【转】VC调试的时候 “没有调试信息,未加载符号”
    常用正则表达式——中文匹配、拉丁匹配
    SQL的经典操作——批量复制同行的其它列数据到其它列数据
    关于C++对汉字拼音的处理(3)
    关于环境变量设置是否需要重启的问题
  • 原文地址:https://www.cnblogs.com/loaderman/p/10212875.html
Copyright © 2011-2022 走看看