我将尝试制作一个温度计。为此,我有一张温度计图片(png文件),我使用ImageView将其插入到布局中。现在我想在这个图片上绘制一条线,以显示温度读数的效果。
以下是我的代码。
main.xml
现在我可以使用这个程序在温度计图像上绘制一条线。但是,在运行时如何更改线的高度呢?由于xml文件中写入了height=100,我不知道该如何更改它。请有人帮帮我。
以下是我的代码。
main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
</ImageView>
<com.focusone.Android.DrawExample.CustomDrawableView
android:id="@+id/custom_drawable_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
height='100'
/>
</AbsoluteLayout>
DrawExample.java
package com.focusone.Android.DrawExample;
public class DrawExample extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
CustomDrawableView.java
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
public CustomDrawableView(Context v,AttributeSet as){
super(v,as);
drawShape(as);
}
public void drawShape(AttributeSet ast) {
int x =45; int y=15 ; int width=5; int height=0 ;
height= Integer.parseInt(ast.getAttributeValue(null, "height"));
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
现在我可以使用这个程序在温度计图像上绘制一条线。但是,在运行时如何更改线的高度呢?由于xml文件中写入了height=100,我不知道该如何更改它。请有人帮帮我。