用java代码实现相对布局
1:扫描指定文件夹下的所有图片,用一个ImageSwitcher显示出来。
2:在ImageSwitcher的底部,添加相应的按钮,可以在各图片之间切换。
package
gto.ImageSwitchTest;
import java.io.File;
import java.io.FileReader;
import android.R.drawable;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.text.AlteredCharSequence;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitchTest extends Activity implements OnClickListener,ViewFactory{
/** Called when the activity is first created. */
private File[] m_imgList;
private ImageSwitcher m_ImageSwitch;
private static int m_picIndex;
private static int IMAGESWITCHER_ID = 0x123456 ;
private static int ButtonStartID = 0x2000000 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// 创建一个相对布局
RelativeLayout main_View = new RelativeLayout( this );
// 创建一个ImageSwitcher
m_ImageSwitch = new ImageSwitcher( this );
// 在相对布局中添加图片切换器
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
main_View.addView(m_ImageSwitch,param);
m_ImageSwitch.setId(IMAGESWITCHER_ID);
m_ImageSwitch.setFactory( this );
File file = Environment.getExternalStorageDirectory();
String strPic = file + " \\mag\\mag01\\ " ;
File magdir = new File(strPic);
m_imgList = magdir.listFiles();
Drawable myDrawable = Drawable.createFromPath(m_imgList[m_picIndex].toString());
m_ImageSwitch.setImageDrawable(myDrawable);
setContentView(main_View);
// 创建按钮
int count = m_imgList.length;
for ( int i = count - 1 ; i >= 0 ; -- i)
{
Button btn = new Button( this );
btn.setId(ButtonStartID + i);
btn.setText(Integer.toString(i));
btn.setOnClickListener( this );
param = new RelativeLayout.LayoutParams( 20 , 20 );
if ( count - 1 == i ){
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
else {
param.addRule(RelativeLayout.LEFT_OF,ButtonStartID + i + 1 );
}
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
btn.setLayoutParams(param);
main_View.addView(btn,param);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
m_picIndex = v.getId() - ButtonStartID;
Drawable myDrawable = Drawable.createFromPath(m_imgList[m_picIndex].toString());
m_ImageSwitch.setImageDrawable(myDrawable);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView( this );
}
}
import java.io.File;
import java.io.FileReader;
import android.R.drawable;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.text.AlteredCharSequence;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitchTest extends Activity implements OnClickListener,ViewFactory{
/** Called when the activity is first created. */
private File[] m_imgList;
private ImageSwitcher m_ImageSwitch;
private static int m_picIndex;
private static int IMAGESWITCHER_ID = 0x123456 ;
private static int ButtonStartID = 0x2000000 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// 创建一个相对布局
RelativeLayout main_View = new RelativeLayout( this );
// 创建一个ImageSwitcher
m_ImageSwitch = new ImageSwitcher( this );
// 在相对布局中添加图片切换器
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
main_View.addView(m_ImageSwitch,param);
m_ImageSwitch.setId(IMAGESWITCHER_ID);
m_ImageSwitch.setFactory( this );
File file = Environment.getExternalStorageDirectory();
String strPic = file + " \\mag\\mag01\\ " ;
File magdir = new File(strPic);
m_imgList = magdir.listFiles();
Drawable myDrawable = Drawable.createFromPath(m_imgList[m_picIndex].toString());
m_ImageSwitch.setImageDrawable(myDrawable);
setContentView(main_View);
// 创建按钮
int count = m_imgList.length;
for ( int i = count - 1 ; i >= 0 ; -- i)
{
Button btn = new Button( this );
btn.setId(ButtonStartID + i);
btn.setText(Integer.toString(i));
btn.setOnClickListener( this );
param = new RelativeLayout.LayoutParams( 20 , 20 );
if ( count - 1 == i ){
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
else {
param.addRule(RelativeLayout.LEFT_OF,ButtonStartID + i + 1 );
}
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
btn.setLayoutParams(param);
main_View.addView(btn,param);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
m_picIndex = v.getId() - ButtonStartID;
Drawable myDrawable = Drawable.createFromPath(m_imgList[m_picIndex].toString());
m_ImageSwitch.setImageDrawable(myDrawable);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView( this );
}
}