我是Android的新手,目前我正在Android设备app中开发设置动态壁纸。所以,我的问题是如何在任何Android设备中设置gif图像作为壁纸,如动态壁纸。根据我的项目,我有一个imageadapter处理 GIF 图像并将 GIF 图像发送到Imageview,一个活动将该GIF图像显示到imageview,在此活动中我有一个名为<的按钮strong>&#34;设置为壁纸&#34; ,我正在使用滑动库在imageview中显示 GIF 图片。
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public ImageAdapter(Context c) {
mContext = c;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(3, 3, 3, 3);
imageView.setMaxHeight(300);
imageView.setMaxWidth(300);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, FullScreenImage.class);
intent.putExtra("imageID", mThumbIds[position]);
mContext.startActivity(intent);
}
});
Animation anim = AnimationUtils.loadAnimation(mContext.getApplicationContext(), R.anim.fly);
imageView.setAnimation(anim);
anim.start();
}
else{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.mipmap.h1, R.mipmap.h2,
R.mipmap.h3, R.mipmap.h4,
R.mipmap.h5,R.mipmap.h6,
R.mipmap.h7,R.mipmap.h8,
R.mipmap.h9,R.mipmap.h10,
R.mipmap.h11,R.drawable.gp3,
R.drawable.gp2,R.drawable.gp,
R.drawable.onehalloween
};
}
活动
public class FullScreenImage extends Activity {
ImageView imageView,imgBack;
Button setWallpaper,save;
Drawable drawable;
Bitmap bitmap;
String ImagePath;
Uri URI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fullscreen_image);
//share = (Button)findViewById(R.id.btnShare);
save = (Button)findViewById(R.id.btnSave);
//Get image fro Image adapter
Bundle bdl = getIntent().getExtras();
final int imageRes = bdl.getInt("imageID");
//Setting up wallpaper
ImageView image = (ImageView) findViewById(R.id.imgDisplay);
image.setImageResource(imageRes);
if (image != null) {
Log.v("Gif....","........" +image);
Glide.with(this).load(imageRes).into(image);
}
setWallpaper = (Button)findViewById(R.id.setWall);
setWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(imageRes);
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.layout_toast2, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//Header back button...for Mainactivity.java
imgBack = (ImageView)findViewById(R.id.imgBack);
imgBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentLog9 = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intentLog9);
finish();
Intent in = new Intent(FullScreenImage.this,ExitDialog.class);
startActivity(in);
}
});
//Save code
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawable = getResources().getDrawable(imageRes);
bitmap = ((BitmapDrawable)drawable).getBitmap();
ImagePath = MediaStore.Images.Media.insertImage(
getContentResolver(),
bitmap,
"imageRes","imageRes"
);
URI = Uri.parse(ImagePath);
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.layout_toast3, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0,0 );
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
});
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotateimage);
image.setAnimation(anim);
anim.start();
}
}
XML
<?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" >
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/header"
android:orientation="horizontal"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical|left">
<ImageView
android:id="@+id/imgBack"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/back"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#ce3c3c"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:layout_below="@+id/header"
android:background="@color/fullimageback"
>
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/header"
android:layout_alignParentBottom="true"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal|center"
android:padding="5dp"
>
<Button
android:id="@+id/setWall"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/wallpaper"
android:textColor="@color/textcolor"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@color/fullimageback"
android:padding="9dp"
android:layout_marginRight="4dp"
/>
<Button
android:id="@+id/btnSave"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Download"
android:textColor="@color/textcolor"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@color/fullimageback"
android:padding="9dp"
android:layout_marginLeft="4dp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
那么,我如何才能实现这种类型的功能。如果有人知道帮助我。 提前谢谢......