下面是從左邊0像素繪製矩形的代碼,但是如何將矩形設置爲從屏幕右側0像素繪製?右邊的繪製矩形
paint.setColor(Color.RED);
canvas.drawRect(0, 0, 5, canvas.getHeight(), paint);
activity_main.xml中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<package.name.customview.Diagram
android:id="@+id/diagram"
android:layout_width="fill_parent"
android:layout_height="52dp" />
</LinearLayout>
Diagram.java
package com.apptacularapps.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class Diagram extends View {
Paint paint = new Paint();
public Diagram(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawRect(0, 0, 5, canvas.getHeight(), paint);
paint.setColor(Color.RED);
canvas.drawRect(canvas.getWidth() - 5, 0, 5, canvas.getHeight(), paint);
}
}
試試我的答案,看看它是否工作 – Prem