问题描述
你好,感谢你的帮助。
请我下面的API演示了FingerPaint演示。
请我需要得到一个喷枪的效果,在这个意义上,当我画过它变得越来越黑的相同点。
请看到的图像:
,你可以看到该中心是黑暗的,因为我通过了在同一地点油漆一次以上。
请我怎么得到相同的效果,越来越黑的斑点,如果画一个以上的时间?
谢谢!
编辑编辑修改
建议
mPaint.setAlpha(0x80的)
样的工作,但只有当我释放触摸,然后触摸一遍,如果我不释放,保持手指没有达到的效果在屏幕上。
的一点是,你没有达到效果,如果你不释放你的手指从屏幕,如果你继续图纸不释放触摸它不会变暗时,油漆过。如果松开触摸,然后再吸引你得到的效果
这是结果,我得到。我不想要的:
这将是理想的结果:
这是采取的形式API演示了code:
公共类FingerPaint延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(新MyView的(这个));
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setDither(真正的);
mPaint.setColor(0x44FF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
}
私人油漆mPaint;
公共类MyView的扩展视图{
私有静态最终浮动MINP = 0.25f;
私有静态最终浮动MAXP = 0.75f;
私人位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人油漆mBitmapPaint;
公共MyView的(语境C){
超级(C);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(的mpath,mPaint);
}
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮X,浮动Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
}
私人无效TOUCH_MOVE(浮X,浮动Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
MX = X;
我= Y;
}
}
私人无效touch_up(){
mPath.lineTo(MX,MY);
//提交路径,我们的屏幕外
mCanvas.drawPath(的mpath,mPaint);
//杀了这个,所以我们不重复抽奖
mPath.reset();
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
浮X = event.getX();
浮动Y = event.getY();
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
touch_start(X,Y);
无效();
打破;
案例MotionEvent.ACTION_MOVE:
TOUCH_MOVE(X,Y);
无效();
打破;
案例MotionEvent.ACTION_UP:
润色();
无效();
打破;
}
返回true;
}
}
}
找到了解决办法。对于那些谁可能会感兴趣:
公共类drawView函数扩展视图{
市民漆mPaint;
私人油漆mPaint1;
私人油漆mPaint2;
私人位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人油漆mBitmapPaint;
公共drawView函数(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
mPaint =新的油漆();
mPaint.setAlpha(0x80的);
mPaint.setAntiAlias(真正的);
mPaint.setDither(真正的);
mPaint.setColor(0x44000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(5);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawColor(0xFF00B8F5);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
}
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮X,浮动Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
//mCanvas.drawPoint(x,Y,mPaint);
}
私人无效TOUCH_MOVE(MotionEvent事件){
浮X = event.getX();
浮动Y = event.getY();
路径将npath =新路径();
npath.moveTo(MX,MY);
npath.lineTo(X,Y);
MX = X;
我= Y;
mCanvas.drawPath(将npath,mPaint);
npath.reset();
//Log.e("","sto disegando);
}
私人无效touch_up(){
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
浮X = event.getX();
浮动Y = event.getY();
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
touch_start(X,Y);
无效();
打破;
案例MotionEvent.ACTION_MOVE:
TOUCH_MOVE(事件);
无效();
打破;
案例MotionEvent.ACTION_UP:
润色();
无效();
打破;
}
返回true;
}
}
编辑:附加快照我的模拟器(Raghunandan)的。我用你的code没有改变,除了增加了笔划宽度,它看起来像下面。
不好看当你画缓慢。
截屏笔画宽度12,如果你画一个striaght线没有问题。但是,当你画曲折,你可以看到它看起来并不好
Hi and thank you for your help.
Please I am following the "FingerPaint" demo in the API Demos.
Please I would need to get an "airbrush" effect, in the sense that when I draw over the same spot it gets darker and darker.
Please see the image:
as you can see the center is darker because I passed with the paint on the same spot more than one time.
Please how do I get the same effect, of getting darker a spot if drawn over more than one time?
Thank you!
EDIT EDIT EDIT
the suggested
mPaint.setAlpha(0x80)
kind of work, but only if I release touch and then touch again, if I do not release and keep the finger on the screen the effect is not reached.
The point is that you do not reach the effect if you do not release your finger from the screen, if you keep on drawing without releasing the touch it doesn't get darker when paint over. If you release the touch and then draw again you get the effect
This is the result I get. And I do not want:
this would be the desired result:
this is is the code taken form the API Demos:
public class FingerPaint extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0x44FF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
}
private Paint mPaint;
public class MyView extends View {
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
}
Found the solution.For those who might be interested:
public class DrawView extends View {
public Paint mPaint;
private Paint mPaint1;
private Paint mPaint2;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public DrawView(Context context, AttributeSet attrs) {
super( context, attrs);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAlpha(0x80);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0x44000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(5);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFF00B8F5);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
//mCanvas.drawPoint(x, y, mPaint);
}
private void touch_move(MotionEvent event) {
float x = event.getX();
float y = event.getY();
Path npath=new Path();
npath.moveTo(mX, mY);
npath.lineTo( x ,y );
mX=x;
mY=y;
mCanvas.drawPath(npath, mPaint);
npath.reset();
//Log.e("","sto disegando");
}
private void touch_up() {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(event);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
Edit: Attaching snap shot of my emulator (Raghunandan). I used your code no changes except increased the stroke width and it looks like below.
Does not look good when you draw slowly.
Screen shot stroke width 12 if you draw a striaght line no problem. But when you draw zig zag you can see it does not look good
这篇关于Android的油漆:如何获得"喷枪"影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!