我發現了一些線條畫的代碼,但它不起作用。任何一個可以幫助我在android中繪製一條線?如何在android中繪製一條線?
這裏是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layoutmain">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
這裏是我的代碼類:
public class DrawPoints extends Activity implements OnTouchListener {
static int i = 0;
static float static_x = 0;
static float static_y = 0;
static float static_x1 = 0;
static float static_y1 = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View topLayout = this.findViewById(R.id.layoutmain);
// register for events for the view, previously
topLayout.setOnTouchListener((OnTouchListener) this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
String tag = null;
if (i == 0) {
static_x = event.getX();
static_y = event.getY();
i = 1;
} else {
static_x1 = event.getX();
static_y1 = event.getY();
}
if (i == 1) {
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
Canvas canvas = new Canvas();
canvas.drawColor(Color.BLUE);
canvas.drawLine(static_x, static_y, static_x1, static_y1, p);
i = 0;
}
return false;
}
}
還可以參考本示例,其是2D圖形的演示:[Android的2D圖形演示](http://marakana.com/tutorials/android/2d-graphics-example.html) – Maverick 2011-11-22 04:52:02