关于与SurfaceView重叠的控件的点击事件处理

yel_hb yel_hb 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先描述背景:新手,纯的。
问题:我在SurfaceView上层放了一个linearlayout,SurfaceView作为底层的控件,用于绘图,linearlayout中有几个button,用于选择画笔的颜色,现在完成了3指点击显示或隐藏linearlayout。但是现在发现上面的按钮没法用。我觉得是没有拦截到点击事件,因为我在surfaceview中onTouchEvent()方法中直接return true了,也就没法传递下去。但是,如果是返回false让事件传递下去的话这个surfaceView中又不能绘图了。想要达到的效果就是在linearlayout显示出来后,点按钮可以选择颜色,而且不影响原来的绘图功能。
....额,不知道有没有表达清楚,望指教。
下面贴代码
布局文件(不知道怎么去实现控件重叠,直接就上framelayout了):
<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" >

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

<LinearLayout
android:id="@+id/layout_top"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/pchoose_bac"
android:visibility="gone" >

<Button
android:id="@+id/btnred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pred" />
<Button
android:id="@+id/btnyell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pyell" />
<Button
android:id="@+id/btngreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pgreen" />
<Button
android:id="@+id/btnblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pblue" />
</LinearLayout>

</FrameLayout>



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() & 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();

/* 获取参数 */
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();
}
//主要是这里不知道怎么处理,如果return false,绘图直接不显示了。
@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;
}
// 记录当前触摸点得当前得坐标
mPosX = x;
mPosY = y;

return true;
}
}

private void onDraw() {
mCanvas.drawColor(Color.WHITE);
// 绘制曲线
mCanvas.drawPath(mPath, mPaint);
mCanvas.drawText("当前触笔X:" + mPosX, 0, 20, mTextPaint);
mCanvas.drawText("当前触笔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 <= 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;
}

}
...全文

相关推荐

在surfaceview上放置控件

在surfaceview上放置控件如按钮等,采用framelayout

Sophy13611942582

Android利用SurfaceView实现简单计时器

自学了android有几个月了,跟着网上的节奏,应该早点写些博客来提高自己的水准的。但苦于技术水准始终不自信(也是不过关的结果吧),就一直只是将自己学习过程中的问题和重要的知识点写在自己的笔记文档中。 但,总感觉一个人写下来成就感还是欠缺了那么一些,而且有些问题及解答方法抛出来,是有可能得到更多好的反馈及解决方案的。于是,本着不作不会死的心态,一步一步在技术成长的道路前行——>这篇博客就是其中一步! 若博客中有些技术知识点有误或者有更优化的解答方案,还望各位小伙伴多多指出。 以下是正题了: 目标:利用SurfaceView实现一个简单的计时器 图示: 描述:1.利用SurfaceView来实

weixin_38722164

Android源码——SurfaceView添加组件view不被组件覆盖.zip

Android源码——SurfaceView添加组件view不被组件覆盖.zip

易小侠

Android上使用ZXing识别条形码与二维码的方法

主要介绍了Android上使用ZXing识别条形码与二维码的方法,需要的朋友可以参考下

weixin_38748875

自己平时做的android相关的总结

声明:这些内容是逐步总结过来的,所以可能有当时的理解不正确,只希望大家能做个参考: 内容如下: 目录 一句话总结汇总: Copy project into workspace 和add project into work set 的含义 数字签名总结 JNI 基础及注意: Ndk的使用方法: Ant 与 android update project 命令行只有在非根盘符上才能运行 android Launcher Android 运行环境搭建 Android:name什么时候加”.” Activity class {package/class} does not exist 问题的解决 Activity 中两次调用OnCreate的原因 ByteBuffer 和 FloatBuffer 的直接分配和非直接分配 Application的使用小总结 “call to OpenGL ES API with no current context (logged once per thread” 问题的解决 2013年9月7日19:15:33:我的平板分辨率很高可是运行public void onSurfaceChanged(GL10 gl, int width, int height)函数时,分辨率只有 455*320,这是为什么? 关于android添加第三方字体的方法 android-apt-compiler: [t1] res\layout\LinearLayout.xml: Invalid file name: must contain only [a-z0-9_.] Buttons in button bars should be borderless android 支持的距离单位 使用adb shell命令进入手机后使用ls命令提示: opendir failed permission denied 使用adb pull 命令提示permission denied Button 中的setLayoutParams使用注意: layout文件夹和raw文件下面的文件读取 Matrix方法中的set方法和post方法 android 中调用drawBitmap时理解dip(屏幕密度)和px(像素)的区别 SQLiteDatabase 的setTransactionSuccessful作用 终于弄明白 paddingleft margineleft layout_gravity 和gravity之间的区别 自定义控件时要注意的问题。 obtainMessage 的作用: FrameLayout 需要注意的地方: EditText 禁止弹出按键盘: 获取控件屏幕位置和窗口位置: 为什么MyAdapater的getView没有被调用 XmlSerializer使用总结: ListView中的Item自定义点击后的背景色的方法。 drawable各个分辨率 fragment 的几种创建方式 fragment第一次使用遇到的问题 activity变身对话框 onMeasure 中的AT_MOST EXACTLY UNSPECIFIED MotionEvent的触发记录 对于Drawable 的 getIntrinsicHeight 和getIntrinsicWidth的理解 IntentService 使用总结: 文件读写总结: AES 解密失败: XML中的include标签加入后崩溃 Button的background标签使图像拉伸的问题 SharedPreferences 的getString 的陷阱 TextView 中的EMS和Maxlength

mars_cheng
评论 4
收藏 已收藏
分享
返回顶部
yel_hb
Android 80,490 成员 91,375 内容