1

如何在android中使用带有可点击区域的图像映射?

我想为我的 android 应用程序渲染图像,并在我的设计类的滚动视图中添加了两个图像。我在不可见的蒙版图像中创建了不同的可点击区域,当我点击这些区域时,我希望我定义的图像出现,但我无法得到任何结果。

这是我的活动课:

 



        ImageView image = (ImageView) findViewById (R.id.image);
        if (image != null) {
            image.setOnTouchListener (this);
        }
        toast ("Touch the screen to discover where the regions are.");
    }
    public boolean onTouch (View v, MotionEvent ev)
    {
        boolean handledHere = true;

        final int action = ev.getAction();

        final int evX = (int) ev.getX();
        final int evY = (int) ev.getY();
        int nextImage = -1;

        ImageView imageView = (ImageView) v.findViewById (R.id.image);
        if (imageView == null) return false;

        Integer tagNum = (Integer) imageView.getTag ();
        int currentResource = (tagNum == null) ? R.drawable.seatmap : tagNum.intValue ();


        switch (action) {
            case MotionEvent.ACTION_DOWN :
                if (currentResource == R.drawable.seatmap) {
                    nextImage = R.drawable.pre1;
                    handledHere = true;
       /*
       } else if (currentResource != R.drawable.p2_ship_default) {
         nextImage = R.drawable.p2_ship_default;
         handledHere = true;
       */
                } else handledHere = true;
                break;

            case MotionEvent.ACTION_UP :
                int touchColor = getHotspotColor (R.id.image_areas, evX, evY);

                ColorTool ct = new ColorTool ();
                int tolerance = 25;
                nextImage = R.drawable.seatmap;
                if (ct.closeMatch (Color.RED, touchColor, tolerance)) nextImage = R.drawable.pre3;
                else if (ct.closeMatch (Color.BLUE, touchColor, tolerance)) nextImage = R.drawable.preorder1;
                else if (ct.closeMatch (Color.YELLOW, touchColor, tolerance)) nextImage = R.drawable.preorder2;
                else if (ct.closeMatch (Color.WHITE, touchColor, tolerance)) nextImage = R.drawable.seatmap;

                // toast ("Current image: " + currentResource + " next: " + nextImage);
                if (currentResource == nextImage) {
                    nextImage = R.drawable.seatmap;
                }
                handledHere = true;
                break;
            default:
                handledHere = false;
        }

        if (handledHere) {

            if (nextImage > 0) {
                imageView.setImageResource (nextImage);
                imageView.setTag (nextImage);
            }
        }
        return handledHere;
    }

    public int getHotspotColor (int hotspotId, int x, int y) {
        ImageView img = (ImageView) findViewById (hotspotId);
        if (img == null) {
            Log.d ("ImageAreasActivity", "Hot spot image not found");
            return 0;
        } else {
            img.setDrawingCacheEnabled(true);
            Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
            if (hotspots == null) {
                Log.d ("ImageAreasActivity", "Hot spot bitmap was not created");
                return 0;
            } else {
                img.setDrawingCacheEnabled(false);
                return hotspots.getPixel(x, y);
            }
        }
    }
    public void toast (String msg)
    {
        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_LONG).show ();
    }




我想在滚动视图内定义两个图像(大小相同),一个可见,一个不可见,这是我的设计类:


    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    android:background="#ffffff"
    tools:context=".MainActivity">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="521dp"
        android:scrollbarAlwaysDrawVerticalTrack="true">

        <ImageView
            android:id="@+id/image_areas"
            android:layout_width="fill_parent"
            android:scaleType="fitCenter"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:contentDescription="Specs"
            android:scrollbars="vertical"
            android:visibility="visible"
            android:src="@drawable/seatmapmask" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:contentDescription="Specs"
            android:scrollbars="vertical"
            android:visibility="visible"
            android:src="@drawable/seatmap" />
    </ScrollView>

    <ImageView
        android:id="@+id/backButton"
        android:layout_width="match_parent"
        android:layout_height="84dp"
        app:srcCompat="@drawable/go_back_up" />

</LinearLayout>





4

0 回答 0