Skip to content

Commit

Permalink
更改版本
Browse files Browse the repository at this point in the history
解决ANR
  • Loading branch information
miuyongjun committed Aug 6, 2017
1 parent ece506a commit 5bec0ce
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 55 deletions.
7 changes: 2 additions & 5 deletions stickerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 11
versionName "1.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand All @@ -26,9 +26,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package miaoyongjun.stickerview;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;

/**
* Author: miaoyongjun
* Date : 17/8/6
*/

public class DrawableSticker extends Sticker {
private Drawable drawable;
private Rect realBounds;

public DrawableSticker(Drawable drawable) {
this.drawable = drawable;
realBounds = new Rect(0, 0, getWidth(), getHeight());
init(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}

@Override
public void draw(@NonNull Canvas canvas) {
canvas.save();
canvas.concat(getMatrix());
drawable.setBounds(realBounds);
drawable.draw(canvas);
canvas.restore();
}

@Override
public int getWidth() {
return drawable.getIntrinsicWidth();
}

@Override
public int getHeight() {
return drawable.getIntrinsicHeight();
}
}
40 changes: 17 additions & 23 deletions stickerview/src/main/java/miaoyongjun/stickerview/Sticker.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package miaoyongjun.stickerview;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.support.annotation.NonNull;

/**
* Author: miaoyongjun
* Date : 17/8/1
*/

public class Sticker {
public abstract class Sticker {

private boolean init;

private Matrix matrix;

private Bitmap src;

private float[] srcPts;

private float[] dst;
Expand All @@ -27,30 +27,32 @@ public class Sticker {
private float minStickerSize;


Sticker(Bitmap src) {
if (src == null) {
throw new RuntimeException("the Sticker's src cannot be null");
}
this.src = src;
public void init(int width, int height) {
matrix = new Matrix();

srcPts = new float[]{0, 0, // 左上
src.getWidth(), 0, // 右上
src.getWidth(), src.getHeight(), // 右下
0, src.getHeight()};
width, 0, // 右上
width, height, // 右下
0, height};
/*
* 原始旋转效果的点 图片中心点和图片的右下角的点
* 触摸时获取到触摸点以及和中心点形成另一组的点
* 之后通过matrix.setPolyToPoly(src, 0, dst, 0, 2) 方法来获取变换后的matrix
*/
rotateSrcPts = new float[]{
src.getWidth() / 2, src.getHeight() / 2,
src.getWidth(), src.getHeight(),
width / 2, height / 2,
width, height,
};
dst = new float[8];
boundPath = new Path();
}

public abstract void draw(@NonNull Canvas canvas);

public abstract int getWidth();

public abstract int getHeight();

public float getMinStickerSize() {
return minStickerSize;
}
Expand All @@ -71,16 +73,8 @@ Matrix getMatrix() {
return matrix;
}

Bitmap getSrc() {
return src;
}

float getBitmapScale() {
return src.getWidth() / (float) src.getHeight();
}

public void setSrc(Bitmap src) {
this.src = src;
return getWidth() / (float) getHeight();
}

float[] getDst() {
Expand Down
48 changes: 25 additions & 23 deletions stickerview/src/main/java/miaoyongjun/stickerview/StickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.Collections;

/**
* Author: miaoyongjun
* Date : 17/8/1
Expand All @@ -30,22 +33,15 @@
public class StickerView extends AppCompatImageView {

private ArrayList<Sticker> mStickers;

private Paint mStickerPaint;

private Bitmap btnDeleteBitmap;

private Bitmap btnRotateBitmap;

private Sticker currentSticker;

private int maxStickerCount;
private float minStickerSizeScale;

private PointF lastPoint;

private TouchState state;

private int maxStickerCount;
private float minStickerSizeScale;
private float imageBeginScale;
private int closeIcon, rotateIcon;
private int closeSize, rotateSize;
Expand Down Expand Up @@ -76,15 +72,14 @@ private void setAttributes(Context context, AttributeSet attrs) {
minStickerSizeScale = typedArray.getFloat(R.styleable.StickerView_m_image_min_size_scale, 0.5f);
closeIcon = typedArray.getResourceId(R.styleable.StickerView_m_close_icon, R.drawable.sticker_closed);
rotateIcon = typedArray.getResourceId(R.styleable.StickerView_m_rotate_icon, R.drawable.sticker_rotate);
closeSize = typedArray.getDimensionPixelSize(R.styleable.StickerView_m_close_icon_size, dip2px(context, 15));
rotateSize = typedArray.getDimensionPixelSize(R.styleable.StickerView_m_rotate_icon_size, dip2px(context, 15));
closeSize = typedArray.getDimensionPixelSize(R.styleable.StickerView_m_close_icon_size, dip2px(context, 20));
rotateSize = typedArray.getDimensionPixelSize(R.styleable.StickerView_m_rotate_icon_size, dip2px(context, 20));
outLineWidth = typedArray.getDimensionPixelSize(R.styleable.StickerView_m_outline_width, dip2px(context, 1));
outLineColor = typedArray.getColor(R.styleable.StickerView_m_outline_color, Color.WHITE);

} finally {
typedArray.recycle();
}

}

private void init(Context context) {
Expand All @@ -110,18 +105,20 @@ private void init(Context context) {

}

public boolean addSticker(@DrawableRes int res) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), res);
return addSticker(bitmap);
}

public boolean addSticker(Bitmap stickerBitmap) {
public boolean addSticker(@DrawableRes int res) {
if (mStickers.size() >= maxStickerCount) {
return false;
}
Sticker bean = new Sticker(stickerBitmap);
mStickers.add(bean);
currentSticker = bean;
Drawable drawable =
ContextCompat.getDrawable(getContext(), res);
return addSticker(drawable);
}

public boolean addSticker(Drawable drawable) {
DrawableSticker drawableSticker = new DrawableSticker(drawable);
mStickers.add(drawableSticker);
currentSticker = drawableSticker;
invalidate();
return true;
}
Expand Down Expand Up @@ -150,16 +147,21 @@ private void drawStickers(Canvas canvas) {
float imageHeight = imageWidth / sticker.getBitmapScale();
float minSize = (float) Math.sqrt(imageWidth * imageWidth + imageHeight * imageHeight);
sticker.setMinStickerSize(minSize * minStickerSizeScale / 2);
sticker.getMatrix().postScale(imageWidth / sticker.getSrc().getWidth(), imageWidth / sticker.getSrc().getWidth());
sticker.getMatrix().postScale(imageWidth / sticker.getWidth(), imageWidth / sticker.getWidth());
sticker.getMatrix().postTranslate(
(getMeasuredWidth() - imageWidth) / 2,
(getMeasuredHeight() - imageHeight) / 2);
sticker.converse();
sticker.setInit(true);
}
canvas.drawBitmap(sticker.getSrc(), sticker.getMatrix(), null);
sticker.draw(canvas);
if (sticker == currentSticker) {
canvas.drawPath(sticker.getBoundPath(), mStickerPaint);
//不能使用 drawPath 否则图片过大时会导致 Path too large to be rendered into a texture
float[] dst = currentSticker.getDst();
canvas.drawLine(dst[0], dst[1], dst[2], dst[3], mStickerPaint);
canvas.drawLine(dst[2], dst[3], dst[4], dst[5], mStickerPaint);
canvas.drawLine(dst[4], dst[5], dst[6], dst[7], mStickerPaint);
canvas.drawLine(dst[6], dst[7], dst[0], dst[1], mStickerPaint);
drawBtn(sticker, canvas);
}
}
Expand Down
8 changes: 4 additions & 4 deletions stickerview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="StickerView">
<!--初始图片大小缩放比例 默认为0.5f 相对于view的宽度-->
<!--初始图片大小缩放比例 默认为0.5f 相对于view的宽度 范围 0到1-->
<attr name="m_image_init_scale" format="float"/>
<!--贴纸的最大数量 默认为20个-->
<attr name="m_max_count" format="integer"/>
<!--贴纸最小值的缩放比例 默认初始图片斜边的二分之一 根据宽度来定制-->
<!--贴纸最小值的缩放比例 默认初始图片斜边的二分之一 根据宽度来定制 范围 0到1-->
<attr name="m_image_min_size_scale" format="float"/>
<!--关闭按钮图标-->
<attr name="m_close_icon" format="reference"/>
<!--旋转按钮图标-->
<attr name="m_rotate_icon" format="reference"/>
<!--关闭按钮图标大小 默认15dp-->
<!--关闭按钮图标大小 默认20dp-->
<attr name="m_close_icon_size" format="dimension"/>
<!--旋转按钮图标大小 默认15dp-->
<!--旋转按钮图标大小 默认20dp-->
<attr name="m_rotate_icon_size" format="dimension"/>
<!--边框宽度 默认1dp-->
<attr name="m_outline_width" format="dimension"/>
Expand Down

0 comments on commit 5bec0ce

Please sign in to comment.