-. Ac01 java
package com.example.kx04;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ac01 extends Activity implements OnClickListener {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private LinearLayout layout;
private GraphicView gv;
@ Override
public void onCreate (Bundle icicle) {
super.onCreate (icicle);
layout = new LinearLayout (this);
layout.setOrientation (LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams (200,
300);
param.leftMargin = 1;
gv = new GraphicView (this);
layout.addView (gv, param);
Button btn = new Button (this);
btn.setText ("Exit");
btn.setOnClickListener (this);
param = new LinearLayout.LayoutParams (WC, WC);
param.leftMargin = 10;
layout.addView (btn, param);
gv.build_model ();
gv.requestFocus ();
}
@ Override
public boolean onKeyDown (int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
gv.MoveDown ();
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
gv.MoveUp ();
if (keyCode == KeyEvent.KEYCODE_Z)
gv.zip_toggle ();
return true;
}
public void onClick (View arg0) {
finish ();
}
}
Reply:
- GraphicView
package com.example.kx04;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class GraphicView extends View {
private Paint paint = new Paint ();
private ObjectMap root, next_node, curr_item, pa;
private MapList show_list, lv_0_coll, lv_1_coll, lv_2_coll;
private int base, x, y, target_index, acc_index;
private class MapList extends ArrayList
private static final long serialVersionUID = 1L;
}
private class ObjectMap extends HashMap
private static final long serialVersionUID = 1L;
public void put_object (String key, Object value) {
super.put (key, value);
}
public void put_string (String key, String value) {
super.put (key, value);
}
public MapList get_sons () {
return (MapList) super.get ("sons");
}
}
GraphicView (Context context) {
super (context);
target_index = 0;
base = 0;
root = getNewNode (null, "goal");
}
@ Override
protected void onDraw (Canvas canvas) {
canvas.drawColor (Color.BLUE);
y = base + 6;
acc_index = 0;
show_list = new MapList ();
this.drawNode_recursive (root, canvas);
}
private void drawNode_recursive (ObjectMap curr_node, Canvas canvas) {
String level = curr_node.get ("level") toString ();.
int k = Integer.valueOf (level);
x = k * 10 + 2;
MapList son_coll = curr_node.get_sons ();
int sz = son_coll.size ();
String plus = "+"; / / zip
if (sz == 0) {
plus = "-";
show_list.add (curr_node);
this.drawNode (x, y, curr_node, plus, canvas);
if (pa! = null)
pa.put_object ("last_pos", new Integer (y));
curr_node.put_object ("last_pos", new Integer (y + 6));
return;
} Else {
plus = curr_node.get ("zip") toString ();.
if (plus == "+") {
show_list.add (curr_node);
this.drawNode (x, y, curr_node, plus, canvas);
pa = (ObjectMap) curr_node.get ("parent");
if (pa! = null)
pa.put_object ("last_pos", new Integer (y));
curr_node.put_object ("last_pos", new Integer (y + 6));
return;
}
show_list.add (curr_node);
this.drawNode (x, y, curr_node, plus, canvas);
pa = (ObjectMap) curr_node.get ("parent");
if (pa! = null)
pa.put ("last_pos", new Integer (y));
curr_node.put_object ("last_pos", new Integer (y + 6));
for (int i = 0; i
y = y + 15;
this.drawNode_recursive (next_node, canvas);
}
}
}
private void drawNode (int x, int y, ObjectMap curr_node, String plus,
Canvas canvas) {
String tx = curr_node.get ("name") toString ();.
paint.setAntiAlias (true);
if (x! = 2) {
paint.setColor (Color.DKGRAY);
canvas.drawLine (x - 5, y + 5, x, y + 5, paint);
pa = (ObjectMap) curr_node.get ("parent");
Integer last_pos = (Integer) pa.get ("last_pos");
int last_y = last_pos;
canvas.drawLine (x - 5, last_y + 4, x - 5, y + 5, paint);
}
paint.setColor (Color.BLACK);
paint.setStrokeWidth (1);
paint.setColor (Color.LTGRAY);
canvas.drawRect (x, y, x + 10, y + 10, paint);
paint.setColor (Color.BLUE);
canvas.drawLine (x + 2, y + 5, x + 8, y + 5, paint);
if (plus == "+")
canvas.drawLine (x + 5, y + 2, x + 5, y + 8, paint);
if (plus == "@")
canvas.drawRect (x + 2, y + 2, x + 8, y + 8, paint);
String level = curr_node.get ("level") toString ();.
int k = Integer.valueOf (level);
switch (k) {
case 0:
paint.setColor (Color.BLACK);
break;
case 1:
paint.setColor (Color.BLUE);
break;
case 2:
paint.setColor (Color.BLACK);
break;
case 3:
paint.setColor (Color.BLUE);
break;
}
if (target_index == acc_index)
paint.setColor (Color.RED);
canvas.drawText (tx, x + 13, y + 8, paint);
acc_index + +;
}
public void build_model () {
root = getNewNode (null, "goal");
lv_0_coll = root.get_sons ();
curr_item = getNewNode (root, "money");
lv_0_coll.add (curr_item);
lv_1_coll = curr_item.get_sons ();
lv_1_coll.add (getNewNode (curr_item, "salary"));
lv_1_coll.add (getNewNode (curr_item, "bonus"));
lv_1_coll.add (getNewNode (curr_item, "extra_bonus"));
curr_item.put_string ("zip", "@");
curr_item = getNewNode (root, "thing");
lv_0_coll.add (curr_item);
lv_1_coll = curr_item.get_sons ();
next_node = getNewNode (curr_item, "few");
lv_1_coll.add (next_node);
lv_2_coll = next_node.get_sons ();
lv_2_coll.add (getNewNode (next_node, "little"));
lv_2_coll.add (getNewNode (next_node, "fewer"));
next_node.put ("zip", "+");
lv_1_coll.add (getNewNode (curr_item, "middle"));
lv_1_coll.add (getNewNode (curr_item, "busy"));
curr_item.put_string ("zip", "@");
curr_item = getNewNode (root, "long");
lv_0_coll.add (curr_item);
lv_1_coll = curr_item.get_sons ();
lv_1_coll.add (getNewNode (curr_item, "short"));
lv_1_coll.add (getNewNode (curr_item, "middle"));
lv_1_coll.add (getNewNode (curr_item, "long"));
curr_item.put_string ("zip", "@");
root.put_string ("zip", "@");
}
private ObjectMap getNewNode (ObjectMap pa, String name) {
String lv = null;
String level = null;
if (pa == null)
level = "0";
else {
lv = pa.get ("level") toString ();.
level = String.valueOf (Integer.valueOf (lv) + 1);
}
ObjectMap m_obj = new ObjectMap ();
m_obj.put_object ("last_pos", new Integer (18));
m_obj.put_string ("level", level);
m_obj.put_string ("name", name);
m_obj.put_object ("parent", pa);
m_obj.put_object ("sons", new MapList ());
m_obj.put_string ("zip", "+");
return m_obj;
}
public void MoveUp () {
target_index -;
this.invalidate ();
}
public void MoveDown () {
target_index + +;
this.invalidate ();
}
public void zip_toggle () {
ObjectMap curr_node = show_list.get (target_index);
String zz = curr_node.get ("zip") toString ();.
if (zz == "@") {
curr_node.put_string ("zip", "+");
this.invalidate ();
}
if (zz == "+") {
curr_node.put_string ("zip", "@");
this.invalidate ();
}
}
}
Reply:
-. AndroidManifest xml
package = "com.example.kx04"
android: versionCode = "1"
android: versionName = "1.0">
android: targetSdkVersion = "18" />
android: icon = "@ drawable / ic_launcher"
android: label = "@ string / app_name"
android: theme = "@ style / AppTheme">
android: layout_height = "wrap_content"
/>
android: label = "@ string / app_name">
Reply:
I find out what the problem of brain damage, I can only CAO, and pits. . . This book is too pit, and others find it a problem to score points, in fact, too simple
Reply:
No setContentView (layout);, it does not show VIEW
No comments:
Post a Comment