android的gallery
A. 如何打开图库android gallery
Android原生内置了很多App,而Gallery为图库,用于操作设备上的图片,它会在开机的时候主动扫描设备上存储的图片,并可以使用Gallery操作它们。既然要使用Gallery,那么先看看它的AndroidManifest.xml清单文件。
<activity android:name="com.android.camera.ImageGallery"
android:label="@string/gallery_label"
android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/ic_launcher_gallery">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/image" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/video" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="vnd.android.cursor.dir/image" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.OPENABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/image" />
</intent-filter>
</activity>
上面是Gallery的AndroidManifest.xml文件中的部分代码,展示了ImageGallery,从众多Intent-filter中可以看出,选取图片应该使用"android.intent.action.PICK",它有两个miniType,"image/*"是用来获取图片的、"video/*"是用来获取视频的。Android中众多Action的字符串其实被封装在Intent类中,android.intent.action.PICK也不例外,它是Intent.ACTION_PICK。
既然知道了启动Gallery的Action,那么再看看ImageGallery.java的源码,找找其中选中图片后的返回值。
private void launchCropperOrFinish(IImage img) {
Bundle myExtras = getIntent().getExtras();
long size = MenuHelper.getImageFileSize(img);
if (size < 0) {
// Return if the image file is not available.
return;
}
if (size > mVideoSizeLimit) {
DialogInterface.OnClickListener buttonListener =
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle(R.string.file_info_title)
.setMessage(R.string.video_exceed_mms_limit)
.setNeutralButton(R.string.details_ok, buttonListener)
.show();
return;
}
String cropValue = myExtras != null ? myExtras.getString("crop") : null;
if (cropValue != null) {
Bundle newExtras = new Bundle();
if (cropValue.equals("circle")) {
newExtras.putString("circleCrop", "true");
}
Intent cropIntent = new Intent();
cropIntent.setData(img.fullSizeImageUri());
cropIntent.setClass(this, CropImage.class);
cropIntent.putExtras(newExtras);
/* pass through any extras that were passed in */
cropIntent.putExtras(myExtras);
startActivityForResult(cropIntent, CROP_MSG);
} else {
Intent result = new Intent(null, img.fullSizeImageUri());
if (myExtras != null && myExtras.getBoolean("return-data")) {
// The size of a transaction should be below 100K.
Bitmap bitmap = img.fullSizeBitmap(
IImage.UNCONSTRAINED, 100 * 1024);
if (bitmap != null) {
result.putExtra("data", bitmap);
}
}
setResult(RESULT_OK, result);
finish();
}
}
以上的ImageGallery.java的部分源码,从setResult()方法可以看出,它返回的Intent包含了选中图片的Uri,它是一个content://开头的内容提供者,并且如果传递过去的Intent的Extra中,包含一个name为"return-data"并且值为true的时候,还会往Extra中写入name为"data"的图片缩略图。
B. 关于android 的gallery 控件的问题
第一个问题你可以gallery.setSelection(index)解决,index自己调试,一般为1或者2就OK了,如果还没到最左边,在适当增加index的值。
第二个问题你可以在adapter类中先定义个list对象,然后再activity中对list设值,当要修改时,只需要修改传入list的值,然后再notifydatachange()就OK了。
C. android Gallery怎么纵向显示
用动画旋转90度就可以纵向显示了,以下函数,RotateAnimation是android提供的类Gallery.startAnimaiton(RotateAnimation a)
D. 怎样向android的Gallery里动态添加图片
Gallery使用大致方法为
1
定义绑定
mGallery
=
(Gallery)findView...
2
定义适配器
MyAdapter
mAdapter
=
new
MyAdapter(.....,list);
注意list
3
为Gallery设置适配器
mGallery.setAdapter(mAdapter);
然后回到问题如何动态添加图片,
list是你的数据源,新加的图片资源应该加到list中,然后刷新Adapter就行了,比如点击某按钮添加某张图片
button.setOnClickListener(new
OnClickListener()
{
@Override
public
void
onClick(View
v)
{
1.添加图片,本地或者网络图片根据你的需要
list.add("picture");
2.刷新适配器
mAdapter.notifyDataSetChanged();
}
});
E. android 如何让gallery自动滚动
让gallery自动滚动可以通过以下两种方法实现:
一、使用Timer和TimerTask类来完成图片的自动定时滚动:
思路:循环调用Gallery类的onFling()方法。
代码:
<span style="white-space:pre"> </span>task = new TimerTask() {
@Override
public void run() {
/**
* 参数1和2:手指在gallery上的动作
* 参数3和4:x方向和y方向的滚动的速度,-数表示向左滚,+数表示向右
*/
gallery.onFling(null, null, -750, 0);
}
};
timer.schele(task, 1000, 5000);
<span style="font-size:32px;"><strong>
</strong></span>
<span style="font-size:32px;"><strong>方式2:</strong></span>
二、使用Handler消息机制完成
思路:子线程内死循环使用handler每隔多长时间向主线程发送消息,通知gallery改变位置。
代码:
子线程部分:
<span style="white-space:pre"> new Thread(new Runnable() {
<span style="white-space:pre"> </span>int flag = 1;
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void run() {
<span style="white-space:pre"> </span>while (isalive) {
<span style="white-space:pre"> </span>//images为装图片的集合
<span style="white-space:pre"> </span>if ((cur_index + 1) == images.size()) {
<span style="white-space:pre"> </span>flag = -1;
<span style="white-space:pre"> </span>} else if (cur_index == 0) {
<span style="white-space:pre"> </span>flag = 1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Message msg = handler.obtainMessage(MSG_UPDATE, cur_index,
<span style="white-space:pre"> </span>0);
<span style="white-space:pre"> </span>handler.sendMessage(msg);
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Thread.sleep(4000);
<span style="white-space:pre"> </span>} catch (InterruptedException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>switch (flag) {
<span style="white-space:pre"> </span>case 1:
<span style="white-space:pre"> </span>cur_index++;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case -1:
<span style="white-space:pre"> </span>cur_index--;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}).start();</span>
主线程部分:
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == MSG_UPDATE) {
gallery.setSelection(msg.arg1);
}
}
};
两种方式的优缺点:
优点:
使用方式1来实现,效果和手指拖动的效果一样,滚动速度可以自己调。
使用方式2来实现,可以使用继承Gallery的子类,重写onFling方法,返回false来实现手指拖动时图片只滚动一张的效果。
缺点:
使用方式1来实现,当你用手指拖动,就会发现图片滚动的太快了,不是一张一张的滚动的,没法使用继承Gallery的子类,重写onFling方法,返回false来实现手
使用方式2来实现 ,效果没那么好看,图片说换就换了,没缓冲,滚动速度没法控制。
F. android中的gallery过时了,现在用什么来替代它
HorizontalScrollView 或者ViewPager。
G. android gallery要怎么初始化
启动代码里初始化Gallery,这里主要用到Gallery控件的 setImageResource()[显示内容] 和 setLayoutParams()[显示属性] 方法。最最关键的是定制适配器 ImageShowAdapter。而实现循环是通过对position求余实现,Gallery开始时会调用getCount()函数,确定有多少个View要绘制,当绘制到最后一个以后就结束了。为了实现循环我们设置getCount()的返回值为Integer的最大值,这样Gallery就可以绘制足够多的View,但是我们设定的资源图片数组大小为4,要想循环当然要用到取模运算啦。
package com.adamhu.dianxiaoer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class EntryActivity extends Activity {
private Gallery mGallery;
private ImageView mImgView;
private ImageShowAdapter mAdapter = new ImageShowAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
mGallery = (Gallery) findViewById(R.id.gallery);
mImgView = (ImageView) findViewById(R.id.image_view);
try {
mGallery.setAdapter(mAdapter);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Integer[] iView = mAdapter.getImagesId();
mImgView.setImageDrawable(getResources().getDrawable(iView[position%iView.length]));
}
});
}
public class ImageShowAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Integer> imgList = new ArrayList<Integer>();
private ArrayList<Object> imgSize = new ArrayList<Object>();
public ImageShowAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mImagesId[position%mImagesId.length]);
i.setLayoutParams(new Gallery.LayoutParams(270,190));
return i;
}
public Integer[] getImagesId(){
return mImagesId;
}
public void setImagesId(Integer[] mImagesId){
this.mImagesId = mImagesId;
}
private Integer mImagesId[] = {
R.drawable.image1,
R.drawable.image10,
R.drawable.image11,
R.drawable.image12
};
}
}
H. 在android系统 里的 gallery什么意思啊
android系统里的“gallery”指的是图库相册。
gallery 表示:n. 画廊;走廊;旁听席;地道;vt. 在?修建走廊;在?挖地道;vi. 挖地道
相关短语
1、art gallery美术馆;画廊
2、photo gallery图片库
3、picture gallery画馆;美术馆
(8)android的gallery扩展阅读:
近义词:n. 画廊;走廊;旁听席;地道 hall、passage、corridor、underground、subway
gallery 来自拉丁语Galilaea, 现巴勒斯坦地名Galilee,原指位于Galilee的教堂门廊,走廊。
双语例句
1、Beforewego tothegallery.
在我们去画廊之前。
2、Isthisyourgallery?
这是你的画廊吗?
3、Sowhat diddadhave todo at thegallery.
那么其实爸爸要去画廊做什么?