UI 绘制及自定义view?
---------------------------------------------------
2.1 绘制相关对象
1)Bitmap 对象
2)Canvas 画板
3)Paint 画笔
4).........
2.2 自定义view
1)自定义类的编写
a)直接或间接继承view
b)添加构造方法及重写相关方法(例如onDraw)
2)属性(Attribute)的自定义(可选)
a)定义属性文件:res/values/attrs.xml
b)属性文件声明属性
<resources>
<declare-styleable name="CircleView">
<attr name="radius" format="integer" />
</declare-styleable>
</resources>
c)在布局文件中给属性赋值(首先要定义命名空间)
d)在自定义View类的构造方法中获得布局文件中定义的属性值
---------------------------------------------------
2.1 绘制相关对象
1)Bitmap 对象
2)Canvas 画板
3)Paint 画笔
4).........
2.2 自定义view
1)自定义类的编写
a)直接或间接继承view
b)添加构造方法及重写相关方法(例如onDraw)
public class MyListView extends ListView {
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec=MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE>>2,//此view高度的最大值
MeasureSpec.AT_MOST);//扩展模式
super.onMeasure(widthMeasureSpec,
heightMeasureSpec);
Log.i("TAG", "onMeasure");
}
}
public class MainActivity extends Activity {
private List<String> list=new ArrayList<String>();
public MainActivity() {
for(char i='A';i<='Z';i++){
list.add(String.valueOf(i));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView sView=(ScrollView) findViewById(R.id.scrollId);
ListView lsv=(ListView) findViewById(R.id.lsvId);
lsv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list));
//平滑滚动到顶点位置。
sView.smoothScrollTo(0, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
-<ScrollView tools:context=".MainActivity" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/scrollId" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
-<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical">
<ImageView android:layout_height="150dp" android:layout_width="match_parent" android:id="@+id/imgId" android:scaleType="fitXY" android:src="@drawable/a"/>
<包名.MyListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/lsvId"/>
</LinearLayout>
</ScrollView>
2)属性(Attribute)的自定义(可选)
a)定义属性文件:res/values/attrs.xml
b)属性文件声明属性
<resources>
<declare-styleable name="CircleView">
<attr name="radius" format="integer" />
</declare-styleable>
</resources>
c)在布局文件中给属性赋值(首先要定义命名空间)
d)在自定义View类的构造方法中获得布局文件中定义的属性值