提交 8bb00a78 编写于 作者: H huangziwei

完成重构整个涂鸦框架,使其更容易拓展

上级 9f3c33b7
......@@ -123,7 +123,7 @@ public class CopyLocation {
public CopyLocation copy() {
CopyLocation copyLocation = new CopyLocation();
copyLocation.mCopyStartX = mCopyStartX;
copyLocation.mCopyStartX = mCopyStartX;
copyLocation.mCopyStartY = mCopyStartY;
copyLocation.mTouchStartX = mTouchStartX;
copyLocation.mTouchStartY = mTouchStartY;
copyLocation.mX = mX;
......
......@@ -25,6 +25,7 @@ import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
......@@ -52,7 +53,7 @@ public class GraffitiActivity extends Activity {
public static final String TAG = "Graffiti";
public final static int DEFAULT_COPY_SIZE = 20; // 默认仿制大小
public final static int DEFAULT_TEXT_SIZE = 16; // 默认文字大小
public final static int DEFAULT_BITMAP_SIZE = 110; // 默认贴图大小
public final static int DEFAULT_BITMAP_SIZE = 80; // 默认贴图大小
public static final int RESULT_ERROR = -111; // 出现错误
......@@ -113,6 +114,7 @@ public class GraffitiActivity extends Activity {
private TextView mPaintSizeView;
private View mBtnColor;
private View mBtnColorContainer;
private View mBtnHidePanel, mSettingsPanel;
private View mShapeModeContainer;
......@@ -464,6 +466,7 @@ public class GraffitiActivity extends Activity {
mGraffiti.invalidate();
}
});
selectorView.setColumnCount(4);
selectorContainer.addView(selectorView);
}
......@@ -497,6 +500,7 @@ public class GraffitiActivity extends Activity {
findViewById(R.id.graffiti_btn_finish).setOnClickListener(mOnClickListener);
findViewById(R.id.graffiti_btn_back).setOnClickListener(mOnClickListener);
mBtnColor = findViewById(R.id.btn_set_color);
mBtnColorContainer = findViewById(R.id.btn_set_color_container);
mBtnColor.setOnClickListener(mOnClickListener);
mSettingsPanel = findViewById(R.id.graffiti_panel);
mColorContainer = findViewById(R.id.graffiti_color_container);
......@@ -571,10 +575,12 @@ public class GraffitiActivity extends Activity {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
v.setPressed(true);
mGraffiti.setShowOriginal(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
v.setPressed(false);
mGraffiti.setShowOriginal(false);
break;
}
......@@ -583,9 +589,9 @@ public class GraffitiActivity extends Activity {
});
mViewShowAnimation = new AlphaAnimation(0, 1);
mViewShowAnimation.setDuration(300);
mViewShowAnimation.setDuration(150);
mViewHideAnimation = new AlphaAnimation(1, 0);
mViewHideAnimation.setDuration(300);
mViewHideAnimation.setDuration(150);
mHideDelayRunnable = new Runnable() {
public void run() {
hideView(mSettingsPanel);
......@@ -617,6 +623,7 @@ public class GraffitiActivity extends Activity {
mDone = false;
if (v.getId() == R.id.btn_pen_hand) {
if (mGraffiti.getPen() != GraffitiPen.HAND) {
mBtnColorContainer.setVisibility(View.VISIBLE);
mShapeModeContainer.setVisibility(View.VISIBLE);
mColorContainer.setVisibility(View.VISIBLE);
mTouchGestureListener.setSelectedItem(null);
......@@ -632,6 +639,7 @@ public class GraffitiActivity extends Activity {
mDone = true;
} else if (v.getId() == R.id.btn_pen_copy) {
if (mGraffiti.getPen() != GraffitiPen.COPY) {
mBtnColorContainer.setVisibility(View.GONE);
mShapeModeContainer.setVisibility(View.VISIBLE);
mColorContainer.setVisibility(View.VISIBLE);
mTouchGestureListener.setSelectedItem(null);
......@@ -642,6 +650,7 @@ public class GraffitiActivity extends Activity {
mDone = true;
} else if (v.getId() == R.id.btn_pen_eraser) {
if (mGraffiti.getPen() != GraffitiPen.ERASER) {
mBtnColorContainer.setVisibility(View.GONE);
mShapeModeContainer.setVisibility(View.VISIBLE);
mColorContainer.setVisibility(View.VISIBLE);
mTouchGestureListener.setSelectedItem(null);
......@@ -652,6 +661,7 @@ public class GraffitiActivity extends Activity {
mDone = true;
} else if (v.getId() == R.id.btn_pen_text) {
if (mGraffiti.getPen() != GraffitiPen.TEXT) {
mBtnColorContainer.setVisibility(View.VISIBLE);
mShapeModeContainer.setVisibility(View.GONE);
mColorContainer.setVisibility(View.VISIBLE);
mTouchGestureListener.setSelectedItem(null);
......@@ -667,6 +677,7 @@ public class GraffitiActivity extends Activity {
mDone = true;
} else if (v.getId() == R.id.btn_pen_bitmap) {
if (mGraffiti.getPen() != GraffitiPen.BITMAP) {
mBtnColorContainer.setVisibility(View.GONE);
mShapeModeContainer.setVisibility(View.GONE);
mColorContainer.setVisibility(View.VISIBLE);
mTouchGestureListener.setSelectedItem(null);
......@@ -700,7 +711,9 @@ public class GraffitiActivity extends Activity {
}
mDone = true;
} else if (v.getId() == R.id.btn_undo) {
mGraffiti.undo();
if(!mGraffiti.undo()){
Toast.makeText(GraffitiActivity.this, "", Toast.LENGTH_SHORT).show();
}
mTouchGestureListener.setSelectedItem(null);
mDone = true;
} else if (v.getId() == R.id.btn_set_color) {
......
......@@ -45,23 +45,43 @@ public class GraffitiOnTouchGestureListener extends TouchGestureDetector.OnTouch
// 动画相关
private ValueAnimator mScaleAnimator;
private float mAnimTransX, mAnimTranY;
private ValueAnimator mRotateAnimator;
private IGraffitiSelectableItem mSelectedItem; // 当前选中的item
private ISelectionListener mListener;
private ISelectionListener mSelectionListener;
private GraffitiView.IGraffitiViewListener mGraffitiViewListener;
public GraffitiOnTouchGestureListener(GraffitiView graffiti, ISelectionListener listener) {
mGraffiti = graffiti;
mCopyLocation = GraffitiPen.COPY.getCopyLocation();
mCopyLocation.reset();
mGraffiti.addGraffitiViewListener(new GraffitiView.GraffitiViewListener() {
mCopyLocation.updateLocation(graffiti.getBitmap().getWidth() / 2, graffiti.getBitmap().getHeight() / 2);
mGraffitiViewListener = new GraffitiView.IGraffitiViewListener() {
@Override
public void onActionOccur(int action, Object obj) {
if (action == GraffitiView.ACTION_ROTATION) {
mCopyLocation.rotatePosition(mGraffiti, (int) obj);
if(mRotateAnimator==null) {
mRotateAnimator = ValueAnimator.ofInt(-90, 0);
mRotateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue();
mGraffiti.setPivotX(mGraffiti.getWidth() / 2);
mGraffiti.setPivotY(mGraffiti.getHeight() / 2);
mGraffiti.setRotation(value);
}
});
mRotateAnimator.setDuration(100);
}
mRotateAnimator.cancel();
mRotateAnimator.start();
}
}
});
mListener = listener;
};
mGraffiti.addGraffitiViewListener(mGraffitiViewListener);
mSelectionListener = listener;
}
public void setSelectedItem(IGraffitiSelectableItem selectedItem) {
......@@ -121,7 +141,6 @@ public class GraffitiOnTouchGestureListener extends TouchGestureDetector.OnTouch
if (!mCopyLocation.isCopying()) {
mCopyLocation.setCopying(true);
mCopyLocation.setStartPosition(mGraffiti.toX(mTouchX), mGraffiti.toY(mTouchY));
// mCurrGraffitiPath.updateCopy(mGraffiti.toX(mTouchX), mGraffiti.toY(mTouchY), mCopyLocation.getX(), mCopyLocation.getY());
}
}
......@@ -231,7 +250,7 @@ public class GraffitiOnTouchGestureListener extends TouchGestureDetector.OnTouch
PointF xy = item.getLocation();
mSelectedItemX = xy.x;
mSelectedItemY = xy.y;
mListener.onSelectedItem(mSelectedItem, true);
mSelectionListener.onSelectedItem(mSelectedItem, true);
break;
}
}
......@@ -239,9 +258,9 @@ public class GraffitiOnTouchGestureListener extends TouchGestureDetector.OnTouch
if (mSelectedItem != null) { // 取消选定
IGraffitiSelectableItem old = mSelectedItem;
setSelectedItem(null);
mListener.onSelectedItem(old, false);
mSelectionListener.onSelectedItem(old, false);
} else {
mListener.onCreateSelectableItem(mGraffiti.toX(mTouchX), mGraffiti.toY(mTouchY));
mSelectionListener.onCreateSelectableItem(mGraffiti.toX(mTouchX), mGraffiti.toY(mTouchY));
}
}
} else {
......@@ -315,11 +334,11 @@ public class GraffitiOnTouchGestureListener extends TouchGestureDetector.OnTouch
}
public void setGraffitiListener(ISelectionListener graffitiListener) {
mListener = graffitiListener;
mSelectionListener = graffitiListener;
}
public ISelectionListener getGraffitiListener() {
return mListener;
return mSelectionListener;
}
public interface ISelectionListener {
......
......@@ -33,9 +33,9 @@ public class GraffitiParams implements Parcelable {
/**
* 涂鸦时(手指按下)隐藏设置面板的延长时间(ms),当小于等于0时则为不尝试隐藏面板(即保持面板当前状态不变);当大于0时表示需要触摸屏幕超过一定时间后才隐藏
* 或者手指抬起时展示面板的延长时间(ms),或者表示需要离开屏幕超过一定时间后才展示
* 默认为400ms
* 默认为250ms
*/
public long mChangePanelVisibilityDelay = 400; //ms
public long mChangePanelVisibilityDelay = 250; //ms
/**
* 设置放大镜的倍数,当小于等于0时表示不使用放大器功能
......
......@@ -42,7 +42,7 @@ public class GraffitiView extends View implements IGraffiti {
public final static float MAX_SCALE = 4f; // 最大缩放倍数
public final static float MIN_SCALE = 0.25f; // 最小缩放倍数
public final static int DEFAULT_SIZE = 8; // 默认画笔大小
public final static int DEFAULT_SIZE = 6; // 默认画笔大小
public static final int ERROR_INIT = -1;
public static final int ERROR_SAVE = -2;
......@@ -89,7 +89,7 @@ public class GraffitiView extends View implements IGraffiti {
private float mGraffitiSizeUnit = 1; // 长度单位,不同大小的图片的长度单位不一样。该单位的意义同dp的作用类似,独立于图片之外的单位长度
private int mGraffitiRotateDegree = 0; // 相对于初始图片旋转的角度
private List<WeakReference<GraffitiViewListener>> mListenerList = new CopyOnWriteArrayList<>();
private List<WeakReference<IGraffitiViewListener>> mListenerList = new CopyOnWriteArrayList<>();
// 手势相关
private IGraffitiTouchDetector mDefaultTouchDetector;
......@@ -880,7 +880,7 @@ public class GraffitiView extends View implements IGraffiti {
*
* @param listener
*/
public void addGraffitiViewListener(GraffitiViewListener listener) {
public void addGraffitiViewListener(IGraffitiViewListener listener) {
if (listener == null || mListenerList.contains(listener)) {
return;
}
......@@ -892,9 +892,9 @@ public class GraffitiView extends View implements IGraffiti {
*
* @param listener
*/
public void removeGraffitiViewListener(GraffitiViewListener listener) {
GraffitiViewListener callBack;
for (WeakReference<GraffitiViewListener> ref : mListenerList) {
public void removeGraffitiViewListener(IGraffitiViewListener listener) {
IGraffitiViewListener callBack;
for (WeakReference<IGraffitiViewListener> ref : mListenerList) {
callBack = ref.get();
if (callBack == null || callBack == listener) {
mListenerList.remove(ref);
......@@ -903,8 +903,8 @@ public class GraffitiView extends View implements IGraffiti {
}
private void notifyActionOccur(int action, Object obj) {
GraffitiViewListener callBack;
for (WeakReference<GraffitiViewListener> ref : mListenerList) {
IGraffitiViewListener callBack;
for (WeakReference<IGraffitiViewListener> ref : mListenerList) {
callBack = ref.get();
if (callBack != null) {
callBack.onActionOccur(action, obj);
......@@ -917,7 +917,7 @@ public class GraffitiView extends View implements IGraffiti {
/**
* 监听涂鸦中的事件
*/
public interface GraffitiViewListener {
public interface IGraffitiViewListener {
/**
* 操作发生后回调
......
......@@ -75,8 +75,8 @@ public class ImageSelectorAdapter extends BaseAdapter {
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.graffiti_imageselector_item, null);
holder = new ViewHolder();
holder.mImage = (ImageView) convertView.findViewById(R.id.image);
holder.mImageSelected = (ImageView) convertView.findViewById(R.id.image_selected);
holder.mImage = (ImageView) convertView.findViewById(R.id.graffiti_image);
holder.mImageSelected = (ImageView) convertView.findViewById(R.id.graffiti_image_selected);
convertView.setTag(holder);
holder.mImage.setTag("" + ++id);
} else {
......
......@@ -64,7 +64,7 @@ public class ImageSelectorView extends FrameLayout implements View.OnClickListen
mMaxCount = 1;
}
mSelectorListener = listener;
mGridView = (GridView) findViewById(R.id.list_image);
mGridView = (GridView) findViewById(R.id.graffiti_list_image);
mBtnEnter = (TextView) findViewById(R.id.btn_enter);
mBtnEnter.setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
......@@ -114,6 +114,18 @@ public class ImageSelectorView extends FrameLayout implements View.OnClickListen
scanImageData();
}
/**
* 列数
* @param count
*/
public void setColumnCount(int count) {
mGridView.setNumColumns(count);
}
public int getColumnCount() {
return mGridView.getNumColumns();
}
// 扫描系统数据库中的图片
private synchronized void scanImageData() {
......
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/graffiti_hide_panel_pressed" android:state_selected="true"/>
<item android:drawable="@drawable/graffiti_hide_panel"/>
</selector>
\ No newline at end of file
......@@ -61,7 +61,7 @@
android:background="#aaaaaa"/>
<GridView
android:id="@+id/list_image"
android:id="@+id/graffiti_list_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
......
......@@ -5,7 +5,7 @@
android:layout_height="match_parent">
<cn.forward.androids.views.RatioImageView
android:id="@+id/image"
android:id="@+id/graffiti_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
......@@ -13,7 +13,7 @@
/>
<ImageView
android:id="@+id/image_selected"
android:id="@+id/graffiti_image_selected"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
......
......@@ -29,7 +29,7 @@
android:layout_marginTop="15dp"
android:background="#101010"/>
<TextView
<cn.forward.androids.views.STextView
android:id="@+id/graffiti_txt_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
......@@ -40,6 +40,7 @@
android:maxLines="1"
android:singleLine="true"
android:text="涂鸦"
app:stv_text_color_pressed="#aaffffff"
android:textColor="#ffffff"
android:textSize="18dp"/>
......@@ -61,7 +62,7 @@
android:layout_height="@dimen/title_bar_height"
android:padding="13dp"
android:scaleType="fitCenter"
android:src="@drawable/graffiti_hide_panel"
android:src="@drawable/graffiti_btn_effect_panel"
android:visibility="visible"
app:miv_is_ignore_alpha="true"
app:miv_mask_color="#a8a8a8"/>
......
......@@ -12,20 +12,20 @@
<include
android:id="@+id/graffiti__title_bar"
layout="@layout/graffiti__title_bar" />
android:id="@+id/graffiti_title_bar"
layout="@layout/graffiti_title_bar" />
<FrameLayout
android:id="@+id/graffiti_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/graffiti__title_bar" />
android:layout_below="@id/graffiti_title_bar" />
<RelativeLayout
android:id="@+id/graffiti_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/graffiti__title_bar">
android:layout_below="@id/graffiti_title_bar">
<LinearLayout
android:layout_width="match_parent"
......@@ -229,7 +229,8 @@
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RelativeLayout
<FrameLayout
android:id="@+id/btn_set_color_container"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/graffiti_btn_effect_rect_line"
......@@ -239,7 +240,7 @@
android:id="@+id/btn_set_color"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</FrameLayout>
<SeekBar
android:id="@+id/paint_size"
......
<resources>
<string name="graffiti_app_name">Graffiti</string>
<string name="graffiti_moving_pic">单指移动、双指缩放</string>
<string name="graffiti_clear_screen">清屏</string>
<string name="graffiti_cant_undo_after_clearing">清屏后不可撤销</string>
<string name="graffiti_saving_picture">保存图片</string>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册