我正在尝试制作壁纸应用程序。我可以设置壁纸但不能设置其实际尺寸的壁纸。应用程序就像当用户点击GridView中的图像时,它将被设置为wallpaper.Here是代码`
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
Integer[] imagesIDs ={
R.drawable.img8};
Bitmap goal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
GridView gridView=(GridView)findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),"pic"+(position+1)+"selected",Toast.LENGTH_SHORT).show();
goal= BitmapFactory.decodeResource(getResources(),imagesIDs[position]);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(goal);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context c) {
context=c;
}
@Override
public int getCount() {
return imagesIDs.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView ==null){
imageView =new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(200,200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(imagesIDs[position]);
return imageView;
}
}
}
答案 0 :(得分:0)
您已将中心裁剪应用于图像视图
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
尝试FIT_XY
代替CENTER_CROP
注意:FIT_XY会改变图像的宽高比。检查link了解更多信息。
答案 1 :(得分:0)
在清单文件中,使用用户权限SET WALLPAPER SIZE HINTS,这将完成工作