- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 AOSP 壁纸选择器中获得了以下代码,我尝试简单地将我的壁纸预览图像显示为平板电脑上的 centerCrop 和手机上的 FitXY,仅仅是因为壁纸图像可能不够大,无法填充 Activity 平板电脑上的屏幕,因此我想居中裁剪它。
现在,我加载中心图像的方式是通过我的 fragment ,并且我没有特定的 ImageView,我可以简单地在 xml 文件之间进行更改(简单的方法)。我只是在寻找实现这一目标的最佳方法的帮助。
这是代码 - (完整代码)
public class WallpaperChooser extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.wallpaper_chooser_base);
Fragment fragmentView = getFragmentManager().findFragmentById(
R.id.wallpaper_chooser_fragment);
if (fragmentView == null) {
DialogFragment fragment = WallpaperChooserDialogFragment
.newInstance();
fragment.show(getFragmentManager(), "dialog");
}
}
<小时/>
public class WallpaperChooserDialogFragment extends DialogFragment implements
AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
private static final String TAG = "MainActivity.WallpaperChooserDialogFragment";
private static final String EMBEDDED_KEY = "org.app.wallpapers."
+ "WallpaperChooserDialogFragment.EMBEDDED_KEY";
private static final String SD = Environment.getExternalStorageDirectory().getAbsolutePath();
private boolean mEmbedded;
private Bitmap mBitmap = null;
private ImageAdapter mAdapter;
private ImageView image;
private ArrayList<Integer> mThumbs;
private ArrayList<Integer> mImages;
private WallpaperLoader mLoader;
private WallpaperDrawable mWallpaperDrawable = new WallpaperDrawable();
public static WallpaperChooserDialogFragment newInstance() {
WallpaperChooserDialogFragment fragment = new WallpaperChooserDialogFragment();
fragment.setCancelable(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.containsKey(EMBEDDED_KEY)) {
mEmbedded = savedInstanceState.getBoolean(EMBEDDED_KEY);
} else {
mEmbedded = isInLayout();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(EMBEDDED_KEY, mEmbedded);
}
private void cancelLoader() {
if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
mLoader.cancel(true);
mLoader = null;
}
}
@Override
public void onDetach() {
super.onDetach();
cancelLoader();
}
@Override
public void onDestroy() {
super.onDestroy();
cancelLoader();
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
/* On orientation changes, the dialog is effectively "dismissed" so this is called
* when the activity is no longer associated with this dying dialog fragment. We
* should just safely ignore this case by checking if getActivity() returns null
*/
Activity activity = getActivity();
if (activity != null) {
activity.finish();
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
findWallpapers();
return null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
findWallpapers();
if (mEmbedded) {
View view = inflater.inflate(R.layout.wallpaper_chooser, container, false);
view.setBackgroundDrawable(mWallpaperDrawable);
final Gallery gallery = (Gallery) view.findViewById(R.id.gallery);
gallery.setCallbackDuringFling(false);
gallery.setOnItemSelectedListener(this);
//gallery.setAdapter(new ImageAdapter(getActivity()));
mAdapter = new ImageAdapter(getActivity());
gallery.setAdapter(mAdapter);
return view;
}
return null;
}
private void selectWallpaper(int position) {
try {
WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
Context.WALLPAPER_SERVICE);
wpm.setResource(mImages.get(position));
Activity activity = getActivity();
activity.setResult(Activity.RESULT_OK);
activity.finish();
} catch (IOException e) {
Log.e(TAG, "Failed to set wallpaper: " + e);
}
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectWallpaper(position);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
//image.startAnimation(animFadeOut);
mLoader.cancel();
}
mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
}
public void onNothingSelected(AdapterView<?> parent) {
}
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = resources.getResourcePackageName(R.array.all_wallpapers);
addWallpapers(resources, packageName, R.array.all_wallpapers);
}
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_thumb",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
private class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
private LayoutInflater mLayoutInflater;
ImageAdapter(Activity activity) {
mLayoutInflater = activity.getLayoutInflater();
}
public int getCount() {
return mThumbs.size();
}
public Bitmap getImage(int i)
{
return getBitmap(((Integer)mImages.get(i)).intValue());
}
public Bitmap getItem(int i)
{
return getBitmap(((Integer)mImages.get(i)).intValue());
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
} else {
//image.startAnimation(animFadeIn);
view = convertView;
}
image = (ImageView) view.findViewById(R.id.wallpaper_image);
int thumbRes = mThumbs.get(position);
image.setImageResource(thumbRes);
Drawable thumbDrawable = image.getDrawable();
if (thumbDrawable != null) {
thumbDrawable.setDither(true);
} else {
Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
+ position);
}
return view;
}
}
class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
BitmapFactory.Options mOptions;
WallpaperLoader() {
mOptions = new BitmapFactory.Options();
mOptions.inDither = false;
mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
@Override
protected Bitmap doInBackground(Integer... params) {
if (isCancelled()) return null;
try {
return BitmapFactory.decodeResource(getResources(),
mImages.get(params[0]), mOptions);
} catch (OutOfMemoryError e) {
return null;
}
}
@Override
protected void onPostExecute(Bitmap b) {
if (b == null) return;
if (!isCancelled() && !mOptions.mCancel) {
// Help the GC
if (mBitmap != null) {
mBitmap.recycle();
}
View v = getView();
if (v != null) {
mBitmap = b;
mWallpaperDrawable.setBitmap(b);
v.postInvalidate();
} else {
mBitmap = null;
mWallpaperDrawable.setBitmap(null);
}
mLoader = null;
} else {
b.recycle();
}
}
void cancel() {
mOptions.requestCancelDecode();
super.cancel(true);
}
}
static class WallpaperDrawable extends Drawable {
Bitmap mBitmap;
int mIntrinsicWidth;
int mIntrinsicHeight;
/* package */void setBitmap(Bitmap bitmap) {
mBitmap = bitmap;
if (mBitmap == null)
return;
mIntrinsicWidth = mBitmap.getWidth();
mIntrinsicHeight = mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas) {
if (mBitmap == null) return;
int width = canvas.getWidth();
int height = canvas.getHeight();
int x = (width - mIntrinsicWidth) / 2;
int y = (height - mIntrinsicHeight) / 2;
canvas.drawBitmap(mBitmap, x, y, null);
}
@Override
public int getOpacity() {
return android.graphics.PixelFormat.OPAQUE;
}
}
private Bitmap getBitmap(int i)
{
System.out.println(i);
if(i != 0)
{
System.out.println("ResourceID != 0");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), i);
PrintStream printstream = System.out;
StringBuilder stringbuilder = new StringBuilder("Bitmap = null = ");
boolean flag;
if(bitmap == null)
flag = true;
else
flag = false;
printstream.println(stringbuilder.append(flag).toString());
if(bitmap != null)
return bitmap;
}
return null;
}
最佳答案
测试设备是平板电脑还是手机?
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
关于java - 如何在平板电脑上显示 Bitmap/Drawable 作为 centerCrop 以及在手机上显示 fitXY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853198/
据我所知,有两种复制位图的方法。 Bitmap.Clone() Bitmap A = new Bitmap("somefile.png"); Bitmap B = (Bitmap)A.Clone();
我收到这个错误,我不知道为什么或不明白原因: vector fourier_descriptor(Gdiplus::Bitmap myBitmap) { vector res;
我试图了解位图原始数据是如何存储的。我读过很多关于位图存储的文章,但有一篇文章指出位图文件的原始位域数据将以相反的顺序 (ABGR) 存储。但是,我找到了另一个显示 ARGB 的图表。因此,我不确定如
Bitmap bmp = BitmapFactory.decodeStream(inputStream, null, op); bmp.getConfig() = null; 为什么bmp.getC
位图中有300帧图像,设置的帧率为30,但视频长度只有3秒;如果有600帧图像,则视频长度为6秒等;视频可以播放所有内容,但播放速度更快,速度是两倍或三倍;原因是什么?应该如何(encodeVideo
List url = new ArrayList(); public Bitmap[] thumbs = { }; 我从我的方法中获取图像fetchImage(String url) for (int
AFAIK 在 Android 上,建议将 Bitmap 对象引用为 WeakReferences 以避免内存泄漏。当不再保留位图对象的硬引用时,垃圾收集器将自动收集它。 现在,如果我理解正确,必须始
我必须从 XML 文件加载图像。 XML 文件中没有关于图像是否为 JPG/GIF/BMP 的信息。加载图像后,我需要将其转换为位图。 有人知道如何在不知道实际文件格式的情况下将图像转换为位图吗?我正
几天前,我们在 Play 商店发布了一个应用程序,它处理高质量的位图并且完全是关于编辑它们。 当我们意识到 20% 的设备出现内存不足错误时,一切进展顺利。所以我们检查了我们的代码,发现 Androi
您好,我已经加载了位图,我需要设置自己的高度和宽度, bitmap.height = 100; 但是这个声明不允许我因为它说 'System.Drawing.Image.Width' cannot b
这是我写的测试,目前会失败: var unusableColor = Color.FromArgb(13, 19, 20, 19); var retrievedColor = Color.Empty;
我是否还需要在 Bitmap.Recycle() 之后调用 Bitmap.Dispose()?或者只是 Bitmap.Dispose() 就足够了? 最佳答案 根据 Android 文档 Bitmap
我试图将所有小图像(如草、水和沥青等)放入一张位图中。 我有一个这样的数组: public int Array[]={3, 1, 3, 3, 1, 1, 3, 3, 3, 3,
开发人员 website简单地说 getHeight() 将返回位图的高度,但有人可以告诉我是像素单位还是 dp 单位? 最佳答案 这是像素。在 Java 代码中,您通常使用像素,例如 View 的宽
我正在 F# 中编写一个项目,该项目使用 System.Drawing、NET Standard 2.0 的位图功能,但无法构建该项目,因为类型“Bitmap”不是在“System.Drawing”中
我正在尝试在 Android 中扩展可缩放的图像查看器以并排使用两个图像。为此,我使用了 Bitmap.createBitmap(int, int, Bitmap.Config)。不幸的是,这似乎会使
我正在使用 Java 创建这款 Android 游戏。但是,我加载位图,然后调整它们的大小以适合屏幕等(dpi 不是很准确)。但我的想法也是为具有少量 ram 的设备加载 16b (mBitmapOp
API 26 adds new option Bitmap.Config.HARDWARE: Special configuration, when bitmap is stored only in
11-24 23:19:18.434: ERROR/AndroidRuntime(12660): Uncaught handler: thread main exiting due to uncaug
我有一组位图。它们在某种程度上都是透明的,我事先不知道哪些部分是透明的。我想从排除透明部分但在正方形中的原始位图中创建一个新位图。我认为这张图片解释了这一点: 我知道如何从现有位图中创建位图,但我不知
我是一名优秀的程序员,十分优秀!