我一直在学习Android,但是一直对自定义View不是太熟悉,这几天看了下,分享下:
自定义View从他的实现方式可以分成三类
(1)
自己绘制控件:就是通过继承View来进行绘制,只要继承 了View 复写他的onDraw()方法就可以进行简单的绘制;
例如这样的代码:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mCanvas =canvas;
String string =(String) getText();
canvas.drawText(string, 100, 100, mPaint);
draw(mCanvas,bitmap);
// canvas.drawColor(Color.BLUE);
}
(2)继承控件实现自定义:也就是通过继承Android提供的控件来在原有的基础上进行修改;
贴一个我实现listView活动item的时候显示删除按钮的的代码:
package com.example.myview;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class MyListView extends ListView implements OnTouchListener ,OnGestureListener{
private GestureDetector mGestureDetector; //手势监听
private View delectButton; //放删除按钮的View
private ViewGroup itemLayout; //listView 的item的layout
private int selectedItem; //当前选择的是哪个
private boolean isDelectShow; //删除按钮是否显示
private onDelectListener delectListener; //删除item的监听器
// 程序在进来的时候首先会执行onTouch方法,如果没有按钮显示就交给手势监听处理,然后执行onDown找出点击的item,然后在onFiling中进行空间的添加跟显示,原理简//单
public MyListView(Context context, AttributeSet attrs){
super(context, attrs);
mGestureDetector =new GestureDetector(getContext(),this);
setOnTouchListener(this);
}
public void setDelectListener(onDelectListener listener){
delectListener =listener;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (isDelectShow) {
itemLayout.removeView(delectButton);
delectButton =null;
isDelectShow =false; mGestureDetector.onTouchEvent(arg1);
return false;
}else{
return mGestureDetector.onTouchEvent(arg1);
}
}
@Override
public boolean onDown(MotionEvent arg0) {
if (!isDelectShow) {
selectedItem = pointToPosition((int)arg0.getX(), (int)arg0.getY());
Log.e("position" ,selectedItem+"");
}else {
if (selectedItem != pointToPosition((int)arg0.getX(), (int)arg0.getY())) {
selectedItem = pointToPosition((int)arg0.getX(), (int)arg0.getY());
Log.e("position" ,selectedItem+"");
}
}
return false;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
if (!isDelectShow&&Math.abs(arg2)>Math.abs(arg3)) {
delectButton =LayoutInflater.from(getContext()).inflate(R.layout.button,null);
Button button =(Button)delectButton.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("onClick", "点击");
itemLayout.removeView(delectButton);
delectButton =null;
isDelectShow =false;
delectListener.delectItem(selectedItem);
}
});
itemLayout =(ViewGroup)getChildAt(selectedItem-getFirstVisiblePosition());
RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
itemLayout.addView(delectButton,layoutParams);
isDelectShow =true;
}else {
}
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public interface onDelectListener{
void delectItem(int index );
};
}
(3)组合控件:就是把现有的控件组合起来:
package com.example.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TitleView extends LinearLayout{
private Button button;
private TextView textView;
public TitleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.titlexml, null);
textView =(TextView)findViewById(R.id.titleText);
button =(Button)findViewById(R.id.titleButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getContext(), "点击了我 !!!",3000).show();
}
});
}
public void setButtonText(String s) {
button.setText(s);
}
public void setTextViewText(String s) {
textView.setText(s);
}
}
代码下载:http://download.csdn.net/detail/u012808234/8517413
原文地址:http://blog.csdn.net/u012808234/article/details/44488025