0

这是一个蒙德里安艺术应用程序,它应该将一个矩形分成 2 部分,并为 2 个新孩子提供不同的颜色。当我触摸屏幕时,应用程序正在停止,我已经查看甚至简化了“onTouch”中的代码,以至于它只应该打印一行但仍然不起作用。目前它仅包含 1 个活动和 1 个补充类。此时这是我的代码:

主要活动

package com.example.mondrianmaker;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    int width, height, x, y, color;
    ArrayList<Rectangle> rectangles, childsRect;
    Bitmap bg;
    Canvas canvas;
    Display display;
    LinearLayout ll;
    Point size;
    Paint paint;
    Random rn;
    Rectangle parentRect;
    TextView text;

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

        //***FULL SCREEN***
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //---FULL SCREEN---

        setContentView(R.layout.activity_main);

        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;

        paint = new Paint(); //paint to color rectangle OBLI

        bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bg);

        ll = (LinearLayout) findViewById(R.id.mondrian);

        rectangles = new ArrayList<Rectangle>();

        text = (TextView) findViewById(R.id.coordinates);

        Rectangle parentRect = new Rectangle(0, 0, 480, 800);   //create super parent rectangle
        rectangles.add(parentRect);                             //add super parent rectangle to list
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //Get Coordinate
        x = (int) event.getX();                 
        y = (int) event.getY();


        //set text view to clicked coordinates and number of rectangles on list
        text.setText(x + ", " + y + "      rectangles: "+rectangles.size());

        //set rectangle color *randomly
        setColor();
        text.setText("after setColor(); OK");
        paint.setColor(color);

        //get rectangle clicked
        text.setText("before getRectParent OK");
        parentRect = getRectParent(x, y);
        text.setText(text + "\nafter getRectParent - before make childs OK ");

        //make children of rectangle clicked
        childsRect = makeChilds(parentRect, x, y);

        //check on childs
        text.setText(childsRect.get(1) + " " + childsRect.get(2));

        //get onTouch action
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            text.setText(x + ',' + y);
            ll.setBackgroundDrawable(new BitmapDrawable(bg));
//      case MotionEvent.ACTION_MOVE:
//          ll.setBackgroundDrawable(new BitmapDrawable(bg));
//          System.out.println("22222");
//      case MotionEvent.ACTION_UP:
//          ll.setBackgroundDrawable(new BitmapDrawable(bg));
//          System.out.println("33333");
        }
        return false;
    }

    public ArrayList<Rectangle> makeChilds(Rectangle parent, int x, int y){
        ArrayList<Rectangle> childs = new ArrayList<Rectangle>();
        for(Rectangle rect : rectangles){
            if(x>=rect.getX1() && x<=rect.getX2() && y>=rect.getY1() && y<=rect.getY2()){
            childs.add(new Rectangle(rect.getX1(), rect.getY1(), rect.getX2()/2, rect.getY2()/2));
            childs.add(new Rectangle(rect.getX2()/2, rect.getY2()/2, rect.getX2(), rect.getY2()));
            }
        }
        return childs;
    }

    public Rectangle getRectParent(int x, int y){
        for(Rectangle g : rectangles){
            if (x > g.getX1() && x < g.getX2() && y > g.getY1() && y < g.getY2()){
                return g;
            } 
        }
        return null;
    }

    public void setColor(){
        rn = new Random();

        switch ((int) Math.floor(rn.nextDouble() * 5)) {
        case 0:
            color = Color.BLACK;
            break;
        case 1:
            color = Color.RED;
            break;
        case 2:
            color = Color.YELLOW;
            break;
        case 3:
            color = Color.WHITE;
            break;
        case 4:
            color = Color.GREEN;
            break;
        }
    }

}

矩形类

package com.example.mondrianmaker;

public class Rectangle {
    private int x1, y1, x2, y2;
    public Rectangle(int x1, int y1, int x2, int y2){
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public int getX1(){ return x1; }
    public int getX2(){ return x2; }
    public int getY1(){ return y1; }
    public int getY2(){ return y2; }

    public String toString(){
        return "("+x1+","+y1+")"+"-("+x2+","+y2+")";
    }
}

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mondrianmaker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mondrian"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/coordinates"
        android:text="x, y"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

    <ExpandableListView
        android:id="@+id/rectangles_list"
        android:layout_width="match_parent"
        android:layout_height="10dp" >
    </ExpandableListView>

</LinearLayout>

LogCat - 我无法解释

10-18 01:31:57.014: E/InputEventReceiver(14912): Exception dispatching input event.
10-18 01:31:57.014: E/MessageQueue-JNI(14912): Exception in MessageQueue callback: handleReceiveCallback
10-18 01:31:57.024: E/MessageQueue-JNI(14912): android.content.res.Resources$NotFoundException: String resource ID #0x26f
10-18 01:31:57.024: W/dalvikvm(14912): threadid=1: thread exiting with uncaught exception (group=0x414ce2a0)
10-18 01:31:57.044: E/AndroidRuntime(14912): FATAL EXCEPTION: main
10-18 01:31:57.044: E/AndroidRuntime(14912): android.content.res.Resources$NotFoundException: String resource ID #0x26f
4

2 回答 2

1

您的 makeChilds 函数返回为 null 也许您应该返回子项;

 public ArrayList<Rectangle> makeChilds(Rectangle parent, int x, int y){
        ArrayList<Rectangle> childs = new ArrayList<Rectangle>();
        for(Rectangle rect : rectangles){
            if(x>=rect.getX1() && x<=rect.getX2() && y>=rect.getY1() && y<=rect.getY2()){
                childs.add(rect);
            }
        }
       return childs ;//<-------modify here return null;
    }
于 2014-10-18T05:07:54.467 回答
0

修复它(暂时)。在线: text.setText(x + ',' + y); 连接到 achar不会转换为字符串并引发异常。

于 2014-10-18T05:43:21.773 回答