我想转换我的应用程序的屏幕并将其转换为位图.我已经知道如何将视图转换为位图,但这不是我想要的,因为我的屏幕由许多片段组成.因此,我希望能够采取屏幕并将其转换为位图...帮助非常感谢谢谢.
使用相机意图拍摄照片后,我压缩位图以降低文件大小.问题是压缩后,它会丢失所有EXIF数据.我在检索原始EXIF数据时没有问题,但是,如何在压缩后将此EXIFF数据添加到字节数组中?
(我的猜测是取字节数组并创建一个位图,然后添加旧的EXIF数据,然后再添加到字节数组,但这是一个移动应用程序,我拍了很多图像,所以我不想浪费记忆)
这是我压缩位图并保存到字节数组输出流的代码:
myBitmapImage.compress(Bitmap.CompressFormat.JPEG, 75, outputstream);//Losing EXIF data here
byte[] ba = outputstream.toByteArray(); // How to add EXIF data here ?
Run Code Online (Sandbox Code Playgroud) 我正在尝试将材质文本选择句柄引入我的应用程序。我从 SDK 中获取了中/右/左手柄(位图)和文本光标(9 块)的可绘制对象,并设置:
<item name="android:textSelectHandleLeft">@drawable/text_select_handle_left_mtrl_alpha</item>
<item name="android:textSelectHandleRight">@drawable/text_select_handle_right_mtrl_alpha</item>
<item name="android:textSelectHandle">@drawable/text_select_handle_middle_mtrl_alpha</item>
<item name="android:textCursorDrawable">@drawable/text_cursor_mtrl_alpha</item>
Run Code Online (Sandbox Code Playgroud)
它按预期工作。但是,在 Lollipop 中,这些可绘制对象使用 XML 中的属性着色为特定颜色android:tint,但我无法在 API<21 上使用该属性。所以我尝试在运行时设置滤色器。

文本光标不会着色。我认为这可能是因为它是 9 补丁。如何在运行时过滤 9 补丁可绘制对象?我可能尝试了所有的PorterDuff.Mode。
右/左手柄为黑色,中间手柄为白色。
即,它们都不是我想要的绿色。为什么?
正如您在上面看到的,我在ImageView编辑文本下方设置了四个,但它们却被着色了。
private void setUpTextCursors() {
Drawable left = getResources().getDrawable(R.drawable.text_select_handle_left_mtrl_alpha);
Drawable right = getResources().getDrawable(R.drawable.text_select_handle_right_mtrl_alpha);
Drawable middle = getResources().getDrawable(R.drawable.text_select_handle_middle_mtrl_alpha);
Drawable cursor = getResources().getDrawable(R.drawable.text_cursor_mtrl_alpha);
ColorFilter cf = new PorterDuffColorFilter(mGreenColor, PorterDuff.Mode.SRC_IN);
/**
* tint my ImageViews, but no effect on edit text handles
*/
left.setColorFilter(cf);
right.setColorFilter(cf);
middle.setColorFilter(cf);
/**
* no effect whatsoever …Run Code Online (Sandbox Code Playgroud) 我正在使用Glide加载位图来创建一个gif.
for (int i = 0, count = files.size(); i < count; i++) {
Bitmap img = Glide.with(context)
.load(files.get(i))
.asBitmap()
.centerCrop()
.into(GIF_EXPORT_SIZE / 2, GIF_EXPORT_SIZE / 2)
.get();
// addFrame creates a copy of img, so we can re-use this bitmap
encoder.addFrame(img);
}
Run Code Online (Sandbox Code Playgroud)
我想知道谁负责回收这个Bitmap或者把它放回到Glide BitmapPool中?Glide似乎没有办法自动或通过使用来重复使用它clear().
我看着使用类似的东西直接将Bitmap添加回池中,Glide.get(context).getBitmapPool().put(img)但根据使用BitmapPool的文档直接导致未定义的行为.
有没有办法比较Bitmaps被包裹的两个BitmapDrawable.
如果尺寸不匹配,比较不应该失败,但它应匹配像素和颜色 Bitmap
我不确定Android的原生部分是如何绘制的Bitmap,因为sameAs即使颜色不同也会返回true.
如果大小不同,我可以Bitmap从另一个创建缩放并将它们进行比较.
在我的源代码我用DrawableCompat.setTint用ImageViews Drawable,并在测试代码我加载Drawable从资源和色调也同样如此.
有任何想法吗?我想有测试,验证源Drawable的 ImageView,如果它按下与否,以及基于颜色.
注1:我的drawables是白色的,我使用色调来设置颜色.循环像素Bitmaps不起作用,因为它们在那时是白色的,很可能Android本机端在绘制时使用色调颜色.
注意2:使用compile 'com.android.support:palette-v7:21.0.0'并Palette.from(bitmap).generate();没有帮助,因为返回的调色板有0个样本,因此无法获得任何颜色信息.
这是我目前的匹配器:
public static Matcher<View> withDrawable(final Drawable d) {
return new BoundedMatcher<View, ImageView>(ImageView.class) {
@Override
public boolean matchesSafely(ImageView iv) {
if (d == null) {
return iv.getDrawable() == null;
} else if (iv.getDrawable() == null) {
return false;
}
if (d …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个显示来自在线数据库的图像的应用程序。但我想使用调色板 API 从每个图像中获得鲜艳的色彩。但我想我不知道如何使用 Bitmap decodeResource 方法或 Palette。请指导我如何在在线 URL 或 URI 图像文件中使用带有调色板的 Bitmap factory.decoderResource。
这是我在第 171 行“位图无效”中显示的活动类 recylceview 子类错误中使用的方法:
170 Bitmap photo = BitmapFactory.decodeResource(getResources(), mCursor.getPosition());
171 Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getLightVibrantColor(getApplicationContext().getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
Run Code Online (Sandbox Code Playgroud)
所以这里是错误:
10-29 17:39:42.067 31692-31750/com.example.xyzreader E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
10-29 17:39:42.282 31692-31692/com.example.xyzreader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xyzreader, PID: 31692
java.lang.IllegalArgumentException: Bitmap is not valid
at android.support.v7.graphics.Palette$Builder.<init>(Palette.java:616)
at android.support.v7.graphics.Palette.from(Palette.java:100)
at android.support.v7.graphics.Palette.generateAsync(Palette.java:134)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:171)
at com.example.xyzreader.ui.ArticleListActivity$Adapter.onBindViewHolder(ArticleListActivity.java:125)
at …Run Code Online (Sandbox Code Playgroud) android color-palette bitmapfactory android-bitmap android-palette
我已经实现了下面的代码来缩小图像的纵横比(通过相对于彼此成比例地减小高度/宽度)。这肯定有助于减小上传到我后端的图像的大小,但这并没有考虑到图像的分辨率。我想设置一个硬图像限制,比如 800Kb,如果图像在调整大小后大于 800Kb,则压缩到小于 800Kb 的点。
任何人都有做这样的事情的经验?我很好奇传递给 Bitmap.Compress 方法的质量参数与每个质量百分比减少的文件大小之间有什么关系 - 如果我能获得这些信息,我相信我可以达到我的目标。
提前感谢您的任何帮助,我当前的代码如下,也许它会帮助其他人在未来朝着这个方向前进。
public static void uploadImage(String url, File file, Callback callback, Context context,
IMAGE_PURPOSE purpose) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int maxWidth = 0;
int maxHeight = 0;
int maxSize = 0;
switch(purpose){
case PROFILE:
maxWidth = Constants.MAX_PROFILE_IMAGE_WIDTH;
maxHeight = Constants.MAX_PROFILE_IMAGE_HEIGHT;
maxSize = Constants.MAX_PROFILE_IMAGE_SIZE;
break;
case UPLOAD:
maxWidth = Constants.MAX_UPLOAD_IMAGE_WIDTH;
maxHeight = Constants.MAX_UPLOAD_IMAGE_HEIGHT;
maxSize = Constants.MAX_UPLOAD_IMAGE_SIZE;
break;
}
int newWidth = bitmap.getWidth(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试下载图像,然后将其显示到我的imageView组件.
要下载,我必须使用Asyntask并显示进度条以通知用户.问题是在经过循环以获得计算的进度值后,我从inputStream得到0.
Log.d("is", "" + inputStream.available()); // ---> will have a value
byte[] buffer = new byte[contentLenght];
while ((read = inputStream.read(buffer)) != -1) {
counter += read;
publishProgress(counter);
outputStream.write(buffer,0,read);
}
Log.d("is", "" + inputStream.available()); // -----> will return 0
bmp = BitmapFactory.decodeStream(inputStream); // bmp will be empty
Run Code Online (Sandbox Code Playgroud)
有没有办法获得进度条的计算值,而不是在输入流的末尾得到0值?
我在这里使用Asyntask.
BMP将有一个价值,我这样做的时候
imageView.setImageBitmap(bmp);,将工作只当我删除的循环,只是通话 bmp = BitmapFactory.decodeStream(inputStream);
但是如果我在这之前放一个循环
bmp = BitmapFactory.decodeStream(inputStream);
Run Code Online (Sandbox Code Playgroud)
imageView将不显示任何内容
这是我的完整Asynctask代码包括网络连接
int progressCounter;
int contentLenght;
int counter;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override …Run Code Online (Sandbox Code Playgroud) 当我使用 Android Profiler 时,我注意到图形占用了大量内存(169 mb),这使得应用程序非常慢,我认为这是由位图引起的,所以我删除了应用程序中的所有位图并再次尝试..
我注意到图形仍然占用 60 - 100 mb 的 RAM,我想知道除了位图之外还有什么会导致内存耗尽?(如果有帮助,我的应用程序使用谷歌地图)
我正在尝试从 URL 加载图像并将其设置为墙纸。我在测试中没有遇到任何崩溃,但 Firebase 报告在生产中显示以下内容
Glide.with(this)
.asBitmap()
.load(full)
.dontTransform()
.format(DecodeFormat.PREFER_ARGB_8888)
.apply(new RequestOptions().fitCenter())
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
Bitmap dstBmp;
if (prefs.getBoolean(SETTINGS_SCROLLABLE_WALL_KEY, false)) {
int width = resource.getWidth();
width = (width * displayHeight / resource.getHeight());
dstBmp = Bitmap.createScaledBitmap(resource, width, displayHeight, true);
} else {
dstBmp = ThumbnailUtils.extractThumbnail(resource, sharedpref.DisplayWidth(), sharedpref.DisplayHeight());
}
WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallManager.setBitmap(dstBmp);
fabProgressCircle.beginFinalAnimation();
fab.setClickable(true);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Fullscreen.this, "Error applying image", Toast.LENGTH_SHORT).show();
} …Run Code Online (Sandbox Code Playgroud) android ×10
android-bitmap ×10
bitmap ×2
compression ×1
exif ×1
graphics ×1
image ×1
nine-patch ×1
screen ×1