Sunday, January 10, 2016

With respect to overlapping controls SurfaceView click event handler


This post was last edited by the yel_hb on 2013-04-07 20:32:18
First Description Background: novice, pure.
Question: I have put a top SurfaceView linearlayout, SurfaceView as the underlying control for drawing, linearlayout has several button, used to select the pen color, now completed three finger tap to show or hide linearlayout. But now I can not find the button above to use. I think it is no interceptions to click events, because I onTouchEvent in surfaceview () method directly return true, and also not to pass along. However, if it returns false, then let the event pass along in this surfaceView can not plot a. The desired effect is achieved after linearlayout displayed, the button can choose the color, but does not affect the original drawing functions.
.... Amount, I do not know there is no clear expression, hope the exhibitions.
Paste the code below
Layout file (do not know how to achieve overlapping controls, directly on framelayout a):
 & lt; FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" 
xmlns: msurface = "http://schemas.android.com/apk/res/com.example.testdraw"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent" & gt;

& Lt; FrameLayout
android: layout_width = "fill_parent"
android: layout_height = "fill_parent" & gt;

& Lt; com.example.testdraw.MySurfaceView
android: id = "@ + id / msurfaceview"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
msurface: paintColor = "# 1C86EE" & gt;
& Lt; /com.example.testdraw.MySurfaceView>
& Lt; / FrameLayout & gt;

& Lt; LinearLayout
android: id = "@ + id / layout_top"
android: layout_width = "fill_parent"
android: layout_height = "50dp"
android: background = "@ drawable / pchoose_bac"
android: visibility = "gone" & gt;

& Lt; Button
android: id = "@ + id / btnred"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ drawable / pred" / & gt;
& Lt; Button
android: id = "@ + id / btnyell"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ drawable / pyell" / & gt;
& Lt; Button
android: id = "@ + id / btngreen"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ drawable / pgreen" / & gt;
& Lt; Button
android: id = "@ + id / btnblue"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ drawable / pblue" / & gt;
& Lt; / LinearLayout & gt;

& Lt; / FrameLayout & gt;



 public class MainActivity extends Activity {

private LinearLayout layoutTop;
private static boolean isVisible = false;
private Button btnPaintRed;
private Button btnPaintBlue;
private Button btnPaintYellow;
private Button btnPaintGreen;
private MySurfaceView ms;

Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);

requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView (R.layout.activity_main);

btnPaintRed = (Button) findViewById (R.id.btnred);
btnPaintBlue = (Button) findViewById (R.id.btnred);
btnPaintYellow = (Button) findViewById (R.id.btnred);
btnPaintGreen = (Button) findViewById (R.id.btnred);

ms = (MySurfaceView) findViewById (R.id.msurfaceview);


btnPaintRed.setOnClickListener (new View.OnClickListener () {

Override
public void onClick (View v) {
ms.paintColor = Color.parseColor ("# FF3366");
}
});

...
}

Override
public boolean onTouchEvent (MotionEvent event) {

int pCount = event.getPointerCount ();
if (pCount == 3) {
layoutTop = (LinearLayout) findViewById (R.id.layout_top);

switch (event.getAction () & amp; MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_UP:
if (isVisible == true) {
layoutTop.setVisibility (View.GONE);
isVisible = false;
} Else {
layoutTop.setVisibility (View.VISIBLE);
isVisible = true;
}
break;
}
}
return true;
}
}


 public class MySurfaceView extends SurfaceView implements Callback, Runnable {

public static final int TIME_IN_FRAME = 50;
public int paintColor;

Paint mPaint = null;
Paint mTextPaint = null;
SurfaceHolder mSurfaceHolder = null;

boolean mRunning = false;
Canvas mCanvas = null;
private Path mPath;
private float mPosX, mPosY;

public MySurfaceView (Context context, AttributeSet attrs) {
super (context, attrs);
this.setFocusable (true);
this.setFocusableInTouchMode (true);
mSurfaceHolder = this.getHolder ();
mSurfaceHolder.addCallback (this);
mCanvas = new Canvas ();

/ * Get the parameter * /
TypedArray tay = context.obtainStyledAttributes (attrs, R.styleable.MySurfaceView);
paintColor = tay.getColor (R.styleable.MySurfaceView_paintColor, 0xFFFFFFFF);

mPaint = new Paint ();
//mPaint.setColor(Color.BLUE);
mPaint.setColor (paintColor);
mPaint.setAntiAlias (true);
mPaint.setAlpha (60);
mPaint.setStyle (Paint.Style.STROKE);
mPaint.setStrokeCap (Paint.Cap.ROUND);
mPaint.setStrokeWidth (20);
mPath = new Path ();
mTextPaint = new Paint ();
mTextPaint.setColor (Color.BLACK);
mTextPaint.setTextSize (15);

tay.recycle ();
}
// Mainly here do not know how to deal with, if return false, the drawing does not show up directly.
Override
public boolean onTouchEvent (MotionEvent event) {

mPaint.setColor (paintColor);

int pCount = event.getPointerCount ();
if (pCount == 3) {
return false;
} Else {
int action = event.getAction ();
float x = event.getX ();
float y = event.getY ();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo (x, y);
break;
case MotionEvent.ACTION_MOVE:
mPath.quadTo (mPosX, mPosY, x, y);
break;
case MotionEvent.ACTION_UP:
// MPath.reset ();
break;
}
// Get the current record of the current touch point coordinates obtained
mPosX = x;
mPosY = y;

return true;
}
}

private void onDraw () {
mCanvas.drawColor (Color.WHITE);
// Draw curve
mCanvas.drawPath (mPath, mPaint);
mCanvas.drawText ("current stylus X:" + mPosX, 0, 20, mTextPaint);
mCanvas.drawText ("Current Stylus Y:" + mPosY, 0, 40, mTextPaint);
}

public void run () {
// TODO Auto-generated method stub
while (mRunning) {
long startTime = System.currentTimeMillis ();
synchronized (mSurfaceHolder) {
mCanvas = mSurfaceHolder.lockCanvas ();
onDraw ();
mSurfaceHolder.unlockCanvasAndPost (mCanvas);
}
long endTime = System.currentTimeMillis ();
int diffTime = (int) (endTime - startTime);
while (diffTime & lt; = TIME_IN_FRAME) {
diffTime = (int) (System.currentTimeMillis () - startTime);
Thread.yield ();
}
}
}

Override
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}

Override
public void surfaceCreated (SurfaceHolder holder) {
mRunning = true;
new Thread (this) .start ();
}

Override
public void surfaceDestroyed (SurfaceHolder holder) {
// TODO Auto-generated method stub
mRunning = false;
}

}

Reply:
You can then return false news ontouch's down and then deal with you this surfaceView event
move after
http://blog.csdn.net/shen332401890/article/details/8627165 look at this article, there are several others described in great detail
Reply:
http://blog.csdn.net/shen332401890/article/details/8572732 sent the wrong link is this
Reply:
quote the second floor shen332401890 reply:
http://blog.csdn.net/shen332401890/article/details/8572732 link sent to the wrong is this

Thank you, but it seems or not, are one level down a lot of talk about the event delivery transfer medium, without the same layer. Like I mentioned above this, in the first framelayout Here are two layout, sibling, surfaceview to intercept the click event, but linearlayout the button but not to intercept. It is set to false before useless.
Reply:
The same question Ah, problem solved incorrect? Hug?

No comments:

Post a Comment