我已经制作了一个简单的演示Android应用程序,因为我从API获取了一些图像网址,而我正在用它们填充一个arrayList。我想从服务器上制作一个幻灯片,我已经看到了查看鳍状肢示例,但在我的情况下没有得到如何做到这一点。 的 ViewFlipperAdapter.java
package com.epe.smaniquines.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.epe.smaniquines.R;
import com.epe.smaniquines.adapter.CatalogAdapter.Viewholder;
import com.epe.smaniquines.util.Const;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class FlipperAdapter extends BaseAdapter {
ArrayList<String> urls;
private Context mContext;
private DisplayImageOptions options;
public static ImageLoader imageLoader;
public FlipperAdapter(Context paramContext, ArrayList<String> urls) {
this.urls = urls;
this.mContext = paramContext;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(paramContext));
options = new DisplayImageOptions.Builder().cacheOnDisc(true)
.showStubImage(R.drawable.noimage)
.showImageOnFail(R.drawable.noimage).build();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return urls.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return urls.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
if (paramView == null) {
paramView = localLayoutInflater.inflate(R.layout.raw_flip,
paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.proImg = ((ImageView) paramView
.findViewById(R.id.iv_flip));
paramView.setTag(localViewholder);
} else {
localViewholder = new Viewholder();
localViewholder = (Viewholder) paramView.getTag();
}
imageLoader.displayImage(urls.get(paramInt), localViewholder.proImg,
options);
return paramView;
}
static class Viewholder {
ImageView proImg;
}
}
raw.flip
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp" >
<ImageView
android:id="@+id/iv_flip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
main.java
package com.epe.smaniquines.ui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterViewFlipper;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.epe.smaniquines.R;
import com.epe.smaniquines.adapter.FlipperAdapter;
import com.epe.smaniquines.util.Const;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class DetailsActivity extends Activity implements OnClickListener {
ImageView proImage, ivSave, ivInfo, ivPlay, ivBak, iv_share;
RelativeLayout rl_botm, rl_option;
TextView tv_facebuk, tv_twiter, tv_nothanks, tv_email, tv_save;
String big_img;
ArrayList<String> resultArray;
private DisplayImageOptions options;
public static ImageLoader imageLoader;
RelativeLayout rl_info;
public boolean flag = false;
int i = 0;
private int PicPosition;
private Handler handler = new Handler();
int mFlipping = 0;
ViewFlipper viewFlipper;
Timer timer;
int flagD = 0;
String data;
String shareType;
File image;
@Override
protected 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.activity_detail);
initialize();
/*
* Intent i = getIntent(); data = i.getStringExtra("data"); shareType =
* i.getStringExtra("type");
*/
big_img = getIntent().getStringExtra(Const.TAG_BIG_IMG);
resultArray = getIntent().getStringArrayListExtra("array");
System.out.println("::::::::::::::ArraySize::::::::" + resultArray);
imageLoader.displayImage(big_img, proImage, options);
ivInfo.setOnClickListener(this);
ivPlay.setOnClickListener(this);
tv_email.setOnClickListener(this);
tv_facebuk.setOnClickListener(this);
tv_nothanks.setOnClickListener(this);
tv_save.setOnClickListener(this);
tv_twiter.setOnClickListener(this);
iv_share.setOnClickListener(this);
ivBak.setOnClickListener(this);
// viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
imageLoader.displayImage(big_img, proImage, options);
proImage.postDelayed(swapImage, 3000);
}
public void open(View view) {
/*
* Intent sharingIntent = new Intent(Intent.ACTION_SEND); screenshotUri
* = Uri.parse(big_img); sharingIntent.setType("image/*");
*/
/*
* sharingIntent .putExtra(Intent.EXTRA_TEXT,
* "Body text of the new status");
* sharingIntent.putExtra(Intent.EXTRA_TITLE, "Traffic At");
* sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
* startActivity(Intent.createChooser(sharingIntent,
* "Share image using"));
*/
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello test"); // <- String
Uri screenshotUri = Uri.parse(image.getPath());
shareIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(shareIntent, "Share image using"));
}
void initialize() {
proImage = (ImageView) findViewById(R.id.iv_det);
ivInfo = (ImageView) findViewById(R.id.iv_info);
ivPlay = (ImageView) findViewById(R.id.iv_play);
ivBak = (ImageView) findViewById(R.id.iv_back);
rl_botm = (RelativeLayout) findViewById(R.id.rl_bottom);
rl_option = (RelativeLayout) findViewById(R.id.rl_options);
tv_save = (TextView) findViewById(R.id.tv_save);
tv_email = (TextView) findViewById(R.id.tv_email);
tv_facebuk = (TextView) findViewById(R.id.tv_facebook);
tv_nothanks = (TextView) findViewById(R.id.tv_no_thanks);
tv_twiter = (TextView) findViewById(R.id.tv_twiter);
rl_option.setVisibility(View.GONE);
iv_share = (ImageView) findViewById(R.id.iv_share);
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration
.createDefault(DetailsActivity.this));
rl_info = (RelativeLayout) findViewById(R.id.rl_info);
rl_info.setVisibility(View.GONE);
resultArray = new ArrayList<String>();
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_share:
rl_option.setVisibility(View.VISIBLE);
break;
case R.id.iv_play:
proImage.setVisibility(View.GONE);
AdapterViewFlipper flipper = (AdapterViewFlipper) findViewById(R.id.flipper);
flipper.setAdapter(new FlipperAdapter(DetailsActivity.this,
resultArray));
break;
case R.id.iv_back:
finish();
break;
case R.id.tv_email:
rl_option.setVisibility(View.GONE);
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL,
new String[] { "youremail@yahoo.com" });
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email,
"Choose an Email client :"));
break;
case R.id.tv_save:
save();
rl_option.setVisibility(View.GONE);
break;
case R.id.tv_facebook:
save();
open(v);
rl_option.setVisibility(View.GONE);
break;
case R.id.tv_no_thanks:
rl_option.setVisibility(View.GONE);
break;
case R.id.tv_twiter:
rl_option.setVisibility(View.GONE);
break;
case R.id.iv_info:
rl_option.setVisibility(View.GONE);
if (flag) {
rl_info.setVisibility(View.VISIBLE);
flag = false;
} else {
rl_info.setVisibility(View.GONE);
flag = true;
}
break;
}
}
// slide show..!!!
MediaPlayer introSound, bellSound;
Runnable swapImage = new Runnable() {
@Override
public void run() {
myslideshow();
handler.postDelayed(this, 1000);
}
};
private void myslideshow() {
PicPosition = resultArray.indexOf(big_img);
if (PicPosition >= resultArray.size())
PicPosition = resultArray.indexOf(big_img); // stop
else
resultArray.get(PicPosition);// move to the next gallery element.
}
//
// SAVE TO SD CARD..!!
void save() {
BitmapDrawable drawable = (BitmapDrawable) proImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File dir = new File(sdCardDirectory.getAbsolutePath()
+ "/3sManiquines/");
image = new File(sdCardDirectory, "3s_" + System.currentTimeMillis()
+ ".png");
dir.mkdirs();
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
addImageToGallery(dir + "", DetailsActivity.this);
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}//
public static void addImageToGallery(final String filePath,
final Context context) {
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
values);
}
// facebook...
public void sharetext(String text) // Text to be shared
{
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, "TITLE");
share.putExtra(android.content.Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(share, "Share via"));
finish();
}
public void shareimage(String text) // argument is image file name with
// extention
{
Intent shareimage = new Intent(android.content.Intent.ACTION_SEND);
shareimage.setType("*/*");// for all
shareimage.setClassName("com.android.mms",
"com.android.mms.ui.ComposeMessageActivity");
shareimage.putExtra(Intent.EXTRA_STREAM, resultArray.indexOf(big_img));
startActivity(Intent.createChooser(shareimage, "Share Image"));
finish();
}
}
main.xaml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rl_det_hdr"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/bottom_nav_bg" >
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:background="@drawable/btn_back" />
<TextView
android:id="@+id/titledetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="10dp"
android:text="CATALOG"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_main_det"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/rl_bottom"
android:layout_below="@+id/rl_det_hdr"
android:padding="10dp" >
<ImageView
android:id="@+id/iv_det"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rl_bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/frame"
android:visibility="gone" >
<TextView
android:id="@+id/tv_twiter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Twitter"
android:textColor="#1D88FC"
android:textSize="20dp" />
<View
android:id="@+id/sep1"
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:layout_below="@+id/tv_twiter"
android:background="#cecece" />
<TextView
android:id="@+id/tv_facebook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sep1"
android:gravity="center"
android:padding="10dp"
android:text="Facebook"
android:textColor="#1D88FC"
android:textSize="20dp" />
<View
android:id="@+id/sep2"
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:layout_below="@+id/tv_facebook"
android:background="#cecece" />
<TextView
android:id="@+id/tv_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sep2"
android:gravity="center"
android:padding="10dp"
android:text="Email"
android:textColor="#1D88FC"
android:textSize="20dp" />
<View
android:id="@+id/sep3"
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:layout_below="@+id/tv_email"
android:background="#cecece" />
<TextView
android:id="@+id/tv_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sep3"
android:gravity="center"
android:padding="10dp"
android:text="Save"
android:textColor="#1D88FC"
android:textSize="20dp" />
<View
android:id="@+id/sep4"
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:layout_below="@+id/tv_save"
android:background="#cecece" />
<TextView
android:id="@+id/tv_no_thanks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sep4"
android:gravity="center"
android:padding="10dp"
android:text="No Thanks"
android:textColor="#1D88FC"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rl_bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/frame"
android:visibility="visible" >
<TextView
android:id="@+id/tv_twiter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="10dp"
android:text="Info"
android:textColor="#000000"
android:textSize="16dp" />
<View
android:id="@+id/sep1"
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:layout_below="@+id/tv_twiter"
android:background="#cecece" />
<TextView
android:id="@+id/tv_facebook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sep1"
android:gravity="left"
android:padding="10dp"
android:text="Ead one Eno"
android:textColor="#1D88FC"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/bottom_nav_bg"
android:visibility="visible" >
<ImageView
android:id="@+id/iv_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/play_btn" />
<ImageView
android:id="@+id/iv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/about_us" />
<ImageView
android:id="@+id/iv_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/share_icon" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:2)
AdapterViewFlipper是最好的方法,因为在ViewFlipper中你需要预先定义视图。
在onCreate中:
ArrayList<String> urls;
//Fetch and set urls to this ArrayList;
AdapterViewFlipper flipper = (AdapterViewFlipper) findViewById(R.id.flipper);
flipper.setAdapter(new FlipperAdapter(urls));
FlipperAdapter.java
要从网址加载图片,我建议使用通用图片加载程序库:https://github.com/nostra13/Android-Universal-Image-Loader
public class FlipperAdapter extends BaseAdapter {
ArrayList<String> urls;
public FlipperAdapter(ArrayList<String> urls) {
this.urls = urls;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return urls.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return urls.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (LinearLayout) LayoutInflater.inflate(
R.layout.viewLayout, parent, false);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
imageLoader.displayImage(urls.get(position), image, null);
return convertView;
}
}
编辑1:viewLayout.xml(这将包含您要在adapterViewFlipper中显示的项目的布局)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>