`
老七的米店
  • 浏览: 50224 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Api Demo - .graphics(19)

阅读更多
package com.example.android.apis.graphics;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.*;

public class Patterns extends GraphicsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
    
    private static Bitmap makeBitmap1() {
        Bitmap bm = Bitmap.createBitmap(40, 40, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bm);
        c.drawColor(Color.RED);
        Paint p = new Paint();
        p.setColor(Color.BLUE);
        c.drawRect(5, 5, 35, 35, p);
        return bm;
    }
    
    private static Bitmap makeBitmap2() {
        Bitmap bm = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(Color.GREEN);
        p.setAlpha(0xCC);
        c.drawCircle(32, 32, 27, p);
        return bm;
    }
    
    private static class SampleView extends View {
        private final Shader mShader1;
        private final Shader mShader2;
        private final Paint mPaint;
        private final DrawFilter mFastDF;
        
        private float mTouchStartX;
        private float mTouchStartY;
        private float mTouchCurrX;
        private float mTouchCurrY;
        private DrawFilter mDF;

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG |
                                               Paint.DITHER_FLAG,
                                               0);//不方便给画笔加过滤效果的,可以DrawFilter直接作用于canvas, 这里是增强bitmap效果
    
            mShader1 = new BitmapShader(makeBitmap1(), Shader.TileMode.REPEAT,
                                        Shader.TileMode.REPEAT);//图形阴影(要填充的图形,X方向填充模式,Y方向填充模式。
            mShader2 = new BitmapShader(makeBitmap2(), Shader.TileMode.REPEAT,
                                        Shader.TileMode.REPEAT);
            
            Matrix m = new Matrix();
            m.setRotate(30);
            mShader2.setLocalMatrix(m);//给shader应用形变
            
            mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
        }
        
        @Override protected void onDraw(Canvas canvas) {
            canvas.setDrawFilter(mDF);//给画布增效过滤。

            mPaint.setShader(mShader1);
            canvas.drawPaint(mPaint);//绘制底层方格
            
            canvas.translate(mTouchCurrX - mTouchStartX,
                             mTouchCurrY - mTouchStartY);//移动画布,然后绘制,达到拖动效果

            mPaint.setShader(mShader2);//上层圆形
            canvas.drawPaint(mPaint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mTouchStartX = mTouchCurrX = x;
                    mTouchStartY = mTouchCurrY = y;
                    mDF = mFastDF;
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mTouchCurrX = x;
                    mTouchCurrY = y;
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    mDF = null;
                    invalidate();
                    break;
            }
            return true;
        }
    }
}

 

0
0
分享到:
评论

相关推荐

    ros by example for indigo volume 2

    3.8.2 Patrolling a square using SMACH..........................................................................19 3.8.3 Testing SMACH navigation in the ArbotiX simulator..................................

    Java邮件开发Fundamentals of the JavaMail API

    in the demo directory. Installing JavaMail 1.2 To use the JavaMail 1.2 API, download the JavaMail 1.2 implementation, unbundle the javamail-1_2.zip file, and add the mail.jar file to your ...

    ZendFramework中文文档

    9.4. Zend_Date API Overview 9.4.1. Zend_Date Options 9.4.1.1. Selecting the date format type 9.4.1.2. DST and Date Math 9.4.1.3. Month Calculations 9.4.1.4. Speed up date localization and ...

    VB编程资源大全(英文源码 表单)

    This is an excellent example.<END><br>18 , savepos.zip This will show you how to save and load the last recorded position of your form.<END><br>19 , shapeform.zip This will change the shape of ...

    Bochs - The cross platform IA-32 (x86) emulator

    - Added support for VGA graphics mode with 400 lines (partial fix for SF bug #2948724) - NE2K: Fixed "send buffer" command issue on big endian hosts - USB - converted common USB code plus devices ...

    Android代码-使用SVG实现Drawable切换动画

    The implementation relies on bezier curves instead of SVG graphics, and is compatible below API 19. DEMO APK How? The library provides a few implementations like SearchArrowDrawable, ...

    Visual C++ 编程资源大全(英文源码 其它)

    16.zip Comment / Uncomment macros 命令/反命令宏(5KB)<END><br>17,17.zip Custom built files 自定义生成的文件(5KB)<END><br>18,18.zip Define Method 定义方法(5KB)<END><br>19,19.zip ...

    Visual C++ 编程资源大全(英文源码 图形)

    19.zip Apply a 3D bitmap pattern on text or other shapes 将文字上色(3维的位图模板)(6KB)<END><br>20,20.zip Encapsulated Dib API 压缩设备无关位图的API(5KB)<END><br>21,21.zip An enhanced ...

Global site tag (gtag.js) - Google Analytics