`
holoblog
  • 浏览: 1227936 次
博客专栏
E0fcf0b7-6756-3051-9a54-90b4324c9940
SQL Server 20...
浏览量:18932
文章分类
社区版块
存档分类
最新评论

android_Gallery_Toast_缩略图_效果

 
阅读更多

这几天怎么开始闲着了,没办法,无聊之际,就看很多朋友在网上问有关图片显示的问题,我就在这里简单做了一个DEMO,这个DEMO我使用对话框的方式来显示,主要功能是实现一个图片按钮点击后弹出对话框在对话框中有同感Gallery来显示的图片,使用ImageSwitcher组件来实现交替扩大选择显示的功能,并在对话框中使用了两个按钮,当选择好一个图片点击确定后实现图片绑定到之前的按钮组件上并显示出来,其实这只是一个思路,这样可以绑定到别的任何一个组件上,与之实现组件间控件的交互能力增强的动态视觉,提高给用户体验的最佳方式,还自定义了一个TOAST来实现选择图片并绑定到TOAST的显示效果,希望能供有关需要的朋友做最好的学习参考,哎,最近很倒霉,现在还得解决工作的问题,真是没想到会发生这么多,好好生活吧,代码我就贴下来了,如果有想看实例的请在我的资源里下载源代码:

主要的xml

imageswitch.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageButton
android:id="@+id/show"
android:src="@drawable/item1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop="10px"
android:layout_alignParentLeft="true"
android:spacing="20px"
></Gallery>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery"
>
</ImageSwitcher>
</RelativeLayout>

自定义的Toast
CustomerToast.java

package com.jsd;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
* 自定义的Toast来帮助提示显示
* @author jiangshide
*
*/
public class CustomerToast {

/**
* 定义一个静态的方法来为其他类可以直接调用
* @param context
* @param imageResId
* @param content
* @param duration
*/
public static void myToastShow(Context context,int imagesId,int imageBg,String content,int duration) {
//还是需要初始化Toast来实现操作
Toast customer = new Toast(context);
customer.setGravity(Gravity.CENTER, 50, 100);

LinearLayout toastLayout = new LinearLayout(context);
toastLayout.setOrientation(LinearLayout.HORIZONTAL);
toastLayout.setGravity(Gravity.CENTER_VERTICAL);

ImageView imageView = new ImageView(context);
imageView.setImageResource(imagesId);

toastLayout.addView(imageView);

TextView tv_content = new TextView(context);
tv_content.setText(content);
tv_content.setTextColor(Color.BLACK);
tv_content.setBackgroundResource(imageBg);

toastLayout.addView(tv_content);

customer.setView(toastLayout);

customer.show();
}

}

主要的JAVA类

JsdImages.java:

package com.jsd;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

/**
* 需求小DEMO
* 主要实现图片循环浏览
* 选择图片来绑定到需要的组件来更改图片显示
* 以及自定义Toast来提示显示功能
* @author jiangshide
*
*/
public class JsdImages extends Activity {

private static final String TAG = "JsdImages";

ImageButton show;

/** 图标点击事件 */
ImageButton btn_img;

/** 点击事件后弹出的对话框 */
AlertDialog imageChooseDialog;

/** 设置图片 */
private int[] images = {
R.drawable.icon,//默认情况下为0
R.drawable.icon
,R.drawable.image5
,R.drawable.item1
,R.drawable.item2
,R.drawable.item3
,R.drawable.item4,
R.drawable.item5};
/** 把Gallery设置成为成员变量 */
Gallery gallery;
/** 把ImageSwitcher设置成为成员变量 */
ImageSwitcher is;
/** 定义一个成员变量保存图片位置 */
int imagePosition;//此成员变量在没有赋值的情况下默认值为0

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.imageswitch);
getComponents();//初始化话图片显示效果
}

/**
* 组装组件
*/
public void getComponents(){
show = (ImageButton) this.findViewById(R.id.show);
show.setOnClickListener(new ImageButton.OnClickListener(){
public void onClick(View v) {
//使用了自定义的Toast来提示显示
CustomerToast.myToastShow(JsdImages.this, R.drawable.icon, R.drawable.item1,"为自定义的Toast显示图片...", 1);
initImageChooseDialog();//初始化以对话框的形式显示图片
imageChooseDialog.show();//调用对话框显示方法
}
});
}

/**
* 自己创建一个对话框来显示
*/
private void initImageChooseDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择图像");
//设置对话框按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
show.setImageResource(images[imagePosition%images.length]);
CustomerToast.myToastShow(JsdImages.this, images[imagePosition%images.length], images[imagePosition%images.length], "改变了的TOAST图片位置:"+imagePosition%images.length, 1);
}
});
//设置取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {

}
});
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.imageswitch, null);
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(images.length / 2);//默认指定位置

is = (ImageSwitcher) view.findViewById(R.id.image_switcher);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
imagePosition = position;
is.setImageResource(images[position%images.length]);//解决数据下标越界实现GALLERY循环显示
}

public void onNothingSelected(AdapterView<?> parent) {

}
});
is.setFactory(new MyViewFactory(this));
builder.setView(view);
imageChooseDialog = builder.create();
}

/**
*
* @author jiangshide
*
*/
class ImageAdapter extends BaseAdapter{

private Context mContext;

public ImageAdapter(Context c) {
this.mContext = c;
}

/**
* 循环获得数量来创建View对其展示
*/
public int getCount() {
//return images.length;
return Integer.MAX_VALUE;//使用一个小技巧来实现GALLERY无线循环
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

/**
* getVIew()需要先调用getCount()来获得所需要显示的个数
*/
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(mContext);
//iv.setImageResource(images[position]);//这里可能会出现下标越界:下为解决方案
iv.setImageResource(images[position%images.length]);//解决下标可能越界
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(90,90));
iv.setPadding(15, 10, 15, 10);
return iv;
}
}

/**
* 定义显示大小
* @author jiangshide
*
*/
class MyViewFactory implements ViewFactory{

private Context mContext;

public MyViewFactory(Context c) {
this.mContext = c;
}

public View makeView() {
ImageView iv = new ImageView(mContext);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
return iv;
}

}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics