Android自定义控件(上)— 第一篇博客,嘻嘻
工作学习中,往往我们需求一些组合控件的功能,但是Android sdk中提供的基本控件没有现成拿来可用的,用xml布局实现往往又很复杂,所以我们就有了自己写控件的冲动,废话少说,动手开始。
这一篇中,我们实现的方法是利用一个Layout,里面包含了我们需求的控件和逻辑方法,现用一个例子进行详细介绍。
需求:设计一个Imageview,点击左右,可以进行图片浏览,点击上可以自动浏览,点击下方出现seekbar 进行图片透明度设置,点击中间,可以设置文件夹,我们会遍历这个文件夹下方所有的(jpg,jpeg,png,bmp)格式图片。
第一步.设计所需求的Imgeview的布局,xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dip"
android:layout_height="240dip"
android:background="@drawable/back1"
>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:visibility="gone"
/>
<EditText
android:id="@+id/etPath"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingLeft="50dip">
</EditText>
<ImageButton
android:id="@+id/imageButtonPreous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/etPath"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:src="@android:drawable/ic_media_previous"
android:visibility="visible"
/>
<ImageButton
android:id="@+id/imageButtonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/etPath"
android:layout_marginRight="17dp"
android:src="@android:drawable/ic_media_next"
android:visibility="gone"
/>
<ImageButton
android:id="@+id/imageButtonAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:src="@android:drawable/ic_dialog_dialer"
android:visibility="gone"
/>
</RelativeLayout>
第二步.新建一个类,继承自RelativeLayout,并实现对应的构造器,没什么好说的,直接上代码.
public class MyImageView extends RelativeLayout implements View.OnClickListener{
private View mView;
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
mView = LayoutInflater.from(context).inflate(R.layout.myimageview, this, true);
initWidget();
alarmThread.start();
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
LayoutInflater.from(context).inflate(R.layout.myimageview, this, true);这条语句,加载对应的布局。
initWidget(),是初始化基本控件,alarmThread这个线程负责进行刷新,因为我们的imagebtn,当3S内没有点击事件的时候,就应该隐去,这个线程就是负责检测是否超时,进行刷新。
对于遍历文件夹下面的所有文件,可以通过递归,这里为了方便,就值搜索一个文件夹下面的图片文件。
File file = new File(SD_PATH + path);
if(file.exists() && file.isDirectory())
{
mPathList.clear();
File [] files = file.listFiles(new FileFilter()
{
@Override
public boolean accept(File arg0) {
if(arg0.getName().endsWith("png") || arg0.getName().endsWith("jpeg") || arg0.getName().endsWith("jpg"))
{
return true;
}
return false;
}
});
for(File file2 : files)
{
mPathList.add(file2.getName());
}
}
alarmThread 的代码如下:
private Thread alarmThread = new Thread(new Runnable()
{
long current = 0;
@Override
public void run() {
while(bRun)
{
current = System.currentTimeMillis();
if((current - mLNext) > SPAN && mImageBtnNext.isShown())
{
Message msg0 = new Message();
msg0.arg1 = 0;
myHandler.sendMessage(msg0);
}
if((current - mLPreous) > SPAN && mImageBtnPreous.isShown())
{
Message msg1 = new Message();
msg1.arg1 = 1;
myHandler.sendMessage(msg1);
}
if((current - mLAuto) > SPAN && mImageBtnAuto.isShown())
{
Message msg = new Message();
msg.arg1 = 2;
myHandler.sendMessage(msg);
}
if((current - mLSeekbar) > SPAN && mSeekBar.isShown())
{
Message msg = new Message();
msg.arg1 = 3;
myHandler.sendMessage(msg);
}
if((current - mLEdit) > SPAN && mEtPath.isShown())
{
Message msg = new Message();
msg.arg1 = 4;
myHandler.sendMessage(msg);
}
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
自定义的控件的第一种思路大体就是这样,至于里面的一些逻辑功能模块实现,其实也就是对里面的基本控件的事件进行相应的监听响应,下一篇再见.
浙公网安备 33010602011771号