SweepDemo graphics SweepGradient Matrix

闲着无聊,看了一下android源码,看到sweep效果挺好玩的,于是来跟大家分享一下,在源码里面稍作修改,为了方便大家理解,加了一些注释,也算是自己学习一下了,下面是具体的实现。

一:MainActivity类,主要是实现的图片绘制,从创建变换矩阵Matrix,根据坐标点和颜色值实现图片的绘制SweepGradient,为了能够显示出动画效果,设置setRotate。

package com.example.sweepdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

public class MainActivity extends GraphicsActivity {
    static int width;
    static int height;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取屏幕的高度和宽度
        WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        setContentView(new SampleView(this));
    }
    private static class SampleView extends View {
        private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private float mRotate;
        // 创建变换矩阵
        private Matrix mMatrix = new Matrix();
        private Shader mShader;
        private boolean mDoTiming;
        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            float x = width / 2;
            float y = height / 2;
            // x 渲染中心x点坐标, y渲染中心y点坐标,
            // colors 围绕中心渲染的颜色数组,至少要有两种颜色值.
            // positions相对位置的颜色数组,可为null, 若为null,可为null,颜色沿渐变线均匀分布
            mShader = new SweepGradient(x, y, new int[] { Color.GREEN, Color.RED, Color.YELLOW }, null);
            mPaint.setShader(mShader);
            // 设置抗锯齿效果
            mPaint.setAntiAlias(true);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;
            float x = width / 2;
            float y = height / 2;
            // 画布背景的颜色
            canvas.drawColor(Color.WHITE);
            // 以x,y为中心店作为旋转中心,旋转mRotate°
            mMatrix.setRotate(mRotate, x, y);
            // 对遮罩效果进行变换处理
            mShader.setLocalMatrix(mMatrix);
            // 根据坐标的移动实现旋转效果,此处是以3个度数为标准进行旋转
            mRotate += 3;
            if (mRotate >= 360) {
                mRotate = 0;
            }
            // invalidate()函数的主要作用是请求View树进行重绘
            // 注:requestLayout()和requestFocus()函数也会引起视图重绘(未测试)
            invalidate();
            if (mDoTiming) {
                // 取系统当前时间
                long now = System.currentTimeMillis();
                for (int i = 0; i < 20; i++) {
                    canvas.drawCircle(x, y, 150, paint);
                }
                now = System.currentTimeMillis() - now;
                android.util.Log.d("skia", "sweep ms = " + (now / 20.));
                System.out.println("sweep ms = " + (now / 20.));
            } else {
                canvas.drawCircle(x, y, 150, paint);
            }
        }
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_D:
                mPaint.setDither(!mPaint.isDither());
                invalidate();
                return true;
            case KeyEvent.KEYCODE_T:
                mDoTiming = !mDoTiming;
                invalidate();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    }
}

二:GraphicsActivity类,展示绘制图片

package com.example.sweepdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

public class GraphicsActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @SuppressWarnings("unused")
    @Override
    public void setContentView(View view) {
        if (false) { // set to true to test Picture
            ViewGroup vg = new PictureLayout(this);
            vg.addView(view);
            view = vg;
        }
        super.setContentView(view);
    }
}

三:PictureLayout类,图片的具体实现过程,包括图片的属性设置

package com.example.sweepdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

public class PictureLayout extends ViewGroup {
    private final Picture mPicture = new Picture();
    public PictureLayout(Context context) {
        super(context);
    }
    public PictureLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public void addView(View child) {
        if (getChildCount() > 1) {
            throw new IllegalStateException("PictureLayout can host only one direct child");
        }
        super.addView(child);
    }
    @Override
    public void addView(View child, int index) {
        if (getChildCount() > 1) {
            throw new IllegalStateException("PictureLayout can host only one direct child");
        }
        super.addView(child, index);
    }
    @Override
    public void addView(View child, LayoutParams params) {
        if (getChildCount() > 1) {
            throw new IllegalStateException("PictureLayout can host only one direct child");
        }
        super.addView(child, params);
    }
    @Override
    public void addView(View child, int index, LayoutParams params) {
        if (getChildCount() > 1) {
            throw new IllegalStateException("PictureLayout can host only one direct child");
        }
        super.addView(child, index, params);
    }
    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int count = getChildCount();
        int maxHeight = 0;
        int maxWidth = 0;
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
        maxWidth += getPaddingLeft() + getPaddingRight();
        maxHeight += getPaddingTop() + getPaddingBottom();
        Drawable drawable = getBackground();
        if (drawable != null) {
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }
        setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), resolveSize(maxHeight, heightMeasureSpec));
    }
    private void drawPict(Canvas canvas, int x, int y, int w, int h, float sx, float sy) {
        canvas.save();
        canvas.translate(x, y);
        canvas.clipRect(0, 0, w, h);
        canvas.scale(0.5f, 0.5f);
        canvas.scale(sx, sy, w, h);
        canvas.drawPicture(mPicture);
        canvas.restore();
    }
    @SuppressWarnings("unused")
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));
        mPicture.endRecording();
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        if (false) {
            canvas.drawPicture(mPicture);
        } else {
            drawPict(canvas, 0, 0, x, y, 1, 1);
            drawPict(canvas, x, 0, x, y, -1, 1);
            drawPict(canvas, 0, y, x, y, 1, -1);
            drawPict(canvas, x, y, x, y, -1, -1);
        }
    }
    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        location[0] = getLeft();
        location[1] = getTop();
        dirty.set(0, 0, getWidth(), getHeight());
        return getParent();
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = super.getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final int childLeft = getPaddingLeft();
                final int childTop = getPaddingTop();
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值