提交 e7c6b792 编写于 作者: H huangziwei

MosaicDemo

上级 4152ed45
......@@ -78,7 +78,7 @@ mTouchGestureListener = new DoodleOnTouchGestureListener(mDoodleView, new Doodle
item(如文字,贴图)被选中或取消选中时回调
*/
@Override
public void onSelectedItem(IDoodleSelectableItem selectableItem, boolean selected) {
public void onSelectedItem(IDoodle doodle, IDoodleSelectableItem selectableItem, boolean selected) {
//do something
}
......@@ -87,7 +87,7 @@ mTouchGestureListener = new DoodleOnTouchGestureListener(mDoodleView, new Doodle
点击View中的某个点创建可选择的item(如文字,贴图)时回调
*/
@Override
public void onCreateSelectableItem(float x, float y) {
public void onCreateSelectableItem(IDoodle doodle, float x, float y) {
//do something
/*
if (mDoodle.getPen() == DoodlePen.TEXT) {
......@@ -182,9 +182,9 @@ You can create a customized touch gesture detector like [DoodleTouchDetector](ht
***实现[IDoodleTouchDetector](https://github.com/1993hzw/Doodle/blob/master/doodle/src/main/java/cn/hzw/doodle/core/IDoodleTouchDetector.java)接口用于创建自定义手势识别器,比如DoodleTouchDetector***
For example, what should we do to achieve the Mosaic effect? Later...
For example, what should we do to achieve the Mosaic effect? See **[Mosaic Demo](https://github.com/1993hzw/Doodle/blob/master/app/src/main/java/cn/hzw/doodledemo/MosaicDemo.java)**
***举个例子,如何实现马赛克效果呢?后面再说……***
***举个例子,如何实现马赛克效果呢?详见 [Mosaic Demo](https://github.com/1993hzw/Doodle/blob/master/app/src/main/java/cn/hzw/doodledemo/MosaicDemo.java)***
# The developer 开发者
......
......@@ -15,6 +15,7 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MosaicDemo"/>
</application>
</manifest>
\ No newline at end of file
......@@ -34,6 +34,12 @@ public class MainActivity extends Activity {
ImageSelectorActivity.startActivityForResult(REQ_CODE_SELECT_IMAGE, MainActivity.this, null, false);
}
});
findViewById(R.id.btn_mosaic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), MosaicDemo.class));
}
});
mPath = (TextView) findViewById(R.id.img_path);
}
......
package cn.hzw.doodledemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import cn.hzw.doodle.DoodleColor;
import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodleShape;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleItem;
import cn.hzw.doodle.core.IDoodlePen;
/**
* Mosaic effect
*/
public class MosaicDemo extends Activity {
private DoodleView doodleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mosaic);
// step 1
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.thelittleprince2);
doodleView = new DoodleView(this, bitmap, new IDoodleListener() {
@Override
public void onSaved(IDoodle doodle, Bitmap doodleBitmap, Runnable callback) {
Toast.makeText(MosaicDemo.this, "onSaved", Toast.LENGTH_SHORT).show();
}
@Override
public void onReady(IDoodle doodle) {
doodle.setSize(30 * doodle.getUnitSize());
}
});
// step 2
DoodleOnTouchGestureListener touchGestureListener = new DoodleOnTouchGestureListener(doodleView, null);
DoodleTouchDetector touchDetector = new DoodleTouchDetector(this, touchGestureListener);
doodleView.setDefaultTouchDetector(touchDetector);
// step 3
doodleView.setPen(new MosaicPen());
doodleView.setShape(DoodleShape.HAND_WRITE);
findViewById(R.id.btn_mosaic_x3).performClick(); // see setMosaicLevel(View view)
// step 4
ViewGroup container = findViewById(R.id.doodle_container);
container.addView(doodleView);
}
private DoodleColor getMosaicColor(float scale) {
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
Bitmap mosaicBitmap = Bitmap.createBitmap(doodleView.getBitmap(),
0, 0, doodleView.getBitmap().getWidth(), doodleView.getBitmap().getHeight(), matrix, true);
matrix.reset();
matrix.setScale(1 / scale, 1 / scale);
return new DoodleColor(mosaicBitmap, matrix);
}
/*
Though setting a new pen here is not necessary, the design-based specification should do this.
虽然这里设置新的画笔不是必要的,但是基于设计的规范应该这样做。马赛克画笔在概念上不同于其他画笔,
*/
public void setMosaicLevel(View view) {
if (view.getId() == R.id.btn_mosaic_x1) {
doodleView.setColor(getMosaicColor(0.3f));
} else if (view.getId() == R.id.btn_mosaic_x2) {
doodleView.setColor(getMosaicColor(0.1f));
} else if (view.getId() == R.id.btn_mosaic_x3) {
doodleView.setColor(getMosaicColor(0.03f));
}
}
private static class MosaicPen implements IDoodlePen {
@Override
public void config(IDoodleItem doodleItem, Paint paint) {
}
@Override
public boolean isSelectable() {
return false;
}
@Override
public void drawHelpers(Canvas canvas, IDoodle doodle) {
}
@Override
public IDoodlePen copy() {
return this;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
......@@ -15,8 +14,7 @@
android:id="@+id/btn_select_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择图片"
/>
android:text="@string/doodle_select_image" />
<TextView
android:id="@+id/img_path"
......@@ -28,29 +26,60 @@
android:ellipsize="middle"
android:text=""
android:textColor="#ffffff"
android:textSize="12dp"
/>
android:textSize="12dp" />
<TextView
android:id="@+id/author"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/btn_select_image"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/btn_select_image"
android:gravity="center"
android:text="by hzw\nQQ:154330138"
android:textColor="#ffffff"
android:textSize="16dp"
/>
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="by hzw\nQQ:154330138"
android:textColor="#ffffff"
android:textSize="16dp" />
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/author"
android:layout_below="@id/btn_select_image"/>
android:layout_above="@id/txt_demoe"
android:layout_below="@id/btn_select_image" />
<TextView
android:id="@+id/txt_demoe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/demo"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="5dp"
android:text="Demo"
android:textColor="#888888"
android:textSize="16dp" />
<LinearLayout
android:id="@+id/demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_mosaic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mosaic" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/doodle_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_mosaic_x1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="50dp"
android:onClick="setMosaicLevel"
android:text="x1" />
<Button
android:id="@+id/btn_mosaic_x2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="50dp"
android:onClick="setMosaicLevel"
android:text="x2" />
<Button
android:id="@+id/btn_mosaic_x3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="50dp"
android:onClick="setMosaicLevel"
android:text="x3" />
</LinearLayout>
</FrameLayout>
\ No newline at end of file
<resources>
<string name="app_name">Doodle</string>
</resources>
<resources>
<string name="app_name">Doodle</string>
<string name="mosaic">马赛克</string>
</resources>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册