android - 在android中制作倾斜 View

标签 android

我正在寻找一种在 android 中制作某种倾斜 View 的方法(Imageview、Button 或 frame)

我可以在其上实现 onclick 的任何东西。

Windows Phone 中有这样一些样式:

enter image description here

或者类似这样的东西:

enter image description here

屏幕的每个部分(黑线上方或黑线下方)都是可点击的,并且可以显示新 Activity 。使用交错 GridView 显示具有不同列大小的网格没有问题。 但我想对于这种方法我应该制作服装布局框架而不是网格?

我在 play.google.com 上找到了另一个示例:

enter image description here

它有点像在某处制作封面和图像可以改变但没有点击能力 在每个 View 上。

编辑:Ernir 的部署建议:

public abstract class MainActivity extends Activity implements View.OnTouchListener {
final ImageView iv= (ImageView)findViewById(R.id.img_cut_1);
final ImageView iv2= (ImageView)findViewById(R.id.img_cut_2); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.img_cut_1).setOnTouchListener(new MyTouchListener());

}

private final class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            int vid = v.getId();
            int nTouchX = (int)me.getX();
            int nTouchY = (int)me.getY();
            if (vid == R.id.img_cut_1) {
                Bitmap bm = ((BitmapDrawable)iv.getDrawable()).getBitmap();
                if (bm.getPixel(nTouchX, nTouchY) != 0) {
                   Log.i("MyActivity", "img_cut_1 true");
                    return true;
                }
                return false;
            }
            if (vid == R.id.img_cut_2) {
                Bitmap bm = ((BitmapDrawable)iv2.getDrawable()).getBitmap();
                if (bm.getPixel(nTouchX, nTouchY) != 0) {
                    Log.i("MyActivity", "img_cut_2 true");
                    return true;
                }

                return false;
            }
        }
        return true;
    }
    } 

和布局:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

        <FrameLayout
            android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/img_cut_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:src="@drawable/image_cut_1" />
        <ImageView
            android:id="@+id/img_cut_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:src="@drawable/ic_launcher" />
    </FrameLayout>
    </RelativeLayout>

但出现此错误:

01-06 11:06:33.242: E/AndroidRuntime(15737): FATAL EXCEPTION: main
01-06 11:06:33.242: E/AndroidRuntime(15737): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test.com/com.example.test.com.MainActivity}: java.lang.InstantiationException: com.example.test.com.MainActivity
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.os.Looper.loop(Looper.java:130)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at java.lang.reflect.Method.invokeNative(Native Method)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at java.lang.reflect.Method.invoke(Method.java:507)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at dalvik.system.NativeStart.main(Native Method)
01-06 11:06:33.242: E/AndroidRuntime(15737): Caused by: java.lang.InstantiationException: com.example.test.com.MainActivity
01-06 11:06:33.242: E/AndroidRuntime(15737):    at java.lang.Class.newInstanceImpl(Native Method)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at java.lang.Class.newInstance(Class.java:1409)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-06 11:06:33.242: E/AndroidRuntime(15737):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
01-06 11:06:33.242: E/AndroidRuntime(15737):    ... 11 more

我曾尝试将整个方法添加到另一个 Activity 中,但这没有帮助。 由于您提到的“对所有 ImageViews 使用相同的 OnTouchListener”这个词,我在类中添加了该方法。

最佳答案

最佳解决方案是将标准化图像划分为多边形,其中每个多边形代表一个可点击区域。然后附加一个 OnTouchListener 并检查规范化的触摸坐标是否在多边形内。这个解决方案需要程序员做一些工作,再加上几何算法的实现。我不会进一步介绍这个解决方案,而是向您展示最简单的解决方案,它可以让您花费最少的时间来实现。我猜这就是您想要的,希望我是对的:)。

没有第三方库的最简单的解决方案

请注意,此解决方案的内存效率不高,应谨慎使用

1. 您将图像切割成多个相同大小的图像,其中每个图像仅包含一个可点击区域,其余部分是透明的。如果我以您问题中的第一张图片为例,那么其中一张图片将如下所示:

sample
(来源:magma.is)

2. 现在您将这些图像叠加在多个重叠的 ImageView 中

....
<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img_cut_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/image_cut_1" />
    <ImageView
        android:id="@+id/img_cut_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/image_cut_2" />
...

3. 在您的 Activity/Fragment 中,您为所有 ImageView 设置了相同 OnTouchListener。在那里,您可以通过消除透明度发现真正触摸了哪个 ImageView,然后您将获得有关图像的哪个区域(或按钮,如果您愿意)被按下的所需信息。

@Override
public boolean onTouch(View v, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        int nTouchX = (int)me.getX();
        int nTouchY = (int)me.getY();
        if (v == imageCut1) {
            Bitmap bm = ((BitmapDrawable)imageCut1.getDrawable()).getBitmap();
            if (bm.getPixel(nTouchX, nTouchY) != 0) {
                // non-transparent pixel touched, we found our view, receive further motion events. This can also be set false and code inserted here.
                return true;
            }
            // transparent pixel touched, do not receive further motion events.
            return false;
        }
        if (v == imageCut2) {
            ...

如果您只需要用户点击,您可以继续并行使用 OnClick 或更好地使用 MotionEvent.ACTION_UP 操作并检查点击漂移和向下和向上运动事件之间的最大时间延迟。 MotionEvent.ACTION_UP 中的类似内容:(请注意,只有“正确的”ImageView 才会为您提供此运动事件,因为我们丢弃了其他 View 的进一步运动事件)

if (
    mMainDragStartX <(me.getX()+TAP_DRIFT_TOLERANCE) && 
    mMainDragStartX > (me.getX() -TAP_DRIFT_TOLERANCE) && 
    mMainDragStartY <  (me.getY() + TAP_DRIFT_TOLERANCE) &&
    mMainDragStartY > (me.getY() - TAP_DRIFT_TOLERANCE) &&
    ((SystemClock.elapsedRealtime() - mMainDraggingStarted) < SINGLE_TAP_MAX_TIME)) {
// The user has tapped!
...

关于android - 在android中制作倾斜 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885517/

相关文章:

android - 如何调用 Android 联系人 API

java - onListItemClick 上的 NullPointerException

android - 如何在 Android 中将具有黑色像素的位图转换为另一种颜色?

java - 为什么我的 Android 应用会显示所有 Activity ?

java - 在 Eclipse 上运行 logcat

android - 如何在android中方向改变时继续播放视频

android - 在带有填充角的自定义按钮上添加点击效果

android - 尽管有权限,但写入外部存储的权限被拒绝

android - ListView 中的按钮根据最终列表项被错误修改

java - 删除android中的路径