如何android代码控制图片转换
- 提问者网友:绫月
- 2021-02-04 17:20
- 五星知识达人网友:摆渡翁
- 2021-02-04 17:51
在资源中添加5张图片,重命名为a-e。
实现java代码如下:
packagecn.csdn.android.test;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
publicclassAndroid_test2ActivityextendsActivity{
int[]image=newint[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e
};
intcurrentImg=0;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.id.root);
LinearLayoutmain=(LinearLayout)findViewById(R.id.root);
finalImageViewimages=newImageView(this);
main.addView(images);
images.setImageResource(image[0]);
images.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
if(currentImg>=4){
currentImg=-1;
}
images.setImageResource(image[++currentImg]);
}
});
}
}
- 1楼网友:你哪知我潦倒为你
- 2021-02-04 19:25
package android.apps;
import android.app.activity; import android.content.context; import android.content.res.typedarray; import android.net.uri; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.view.window; import android.view.windowmanager; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview;
public class gallery1 extends activity { private context mcontext; private uri[] uri =getimagesfromsdcard.uriarray; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //影藏顶部程序名称 写在setcontentview(r.layout.gallery_1);之前,不然报错 requestwindowfeature(window.feature_no_title); setcontentview(r.layout.gallery_1); //影藏顶部电量等图标 getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); gallery g = (gallery) findviewbyid(r.id.gallery); g.setadapter(new imageadapter(this)); //设置显示第几张图片 参数是getimagesfromsdcard中的静态变量 g.setselection(getimagesfromsdcard.imageposition); } public class imageadapter extends baseadapter { int mgalleryitembackground; public imageadapter(context c) { mcontext = c; typedarray a = obtainstyledattributes(r.styleable.gallery1); mgalleryitembackground = a.getresourceid( r.styleable.gallery1_android_galleryitembackground, 0); a.recycle(); }
public int getcount() { return uri.length; }
public object getitem(int position) { return position; }
public long getitemid(int position) { return position; }
public view getview(int position, view convertview, viewgroup parent){ imageview view = new imageview(gallery1.this); //设置所有图片的资源地址 view.setimageuri(uri[position]); view.setscaletype(imageview.scaletype.fit_xy); view.setlayoutparams(new gallery.layoutparams(240, 320)); view.setbackgroundresource(mgalleryitembackground); return view; } } }