我正在试图弄清楚如何在android中绘制位图,并为撤消函数保留这些已更改位图的副本.
Bitmap b = ...
Paint p = new Paint();
canvas.drawBitmap(b, new Matrix(), null);
canvas.drawCircle(0,0,20,20);
//does Bitmap b have the circle drawn on it next time?
Run Code Online (Sandbox Code Playgroud)
或者如何在使用画布绘制位图之后获取位图(我想保留一堆位图以及画布绘制所应用的更改)?也许我说这完全错了.
我使用以下内容将jpgImage写入PictureBox.Image.
var jpgImage = new Byte[jpgImageSize];
...
pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));
Run Code Online (Sandbox Code Playgroud)
我可以使用以下内容将字节数组写入文件
using (var bw =
new BinaryWriter(File.Open(filename, FileMode.Create,
FileAccess.Write, FileShare.None)))
{
bw.Write(jpgImage);
}
Run Code Online (Sandbox Code Playgroud)
但是如何从PictureBox.Image中获取jpgImage字节数组,以便将其写入文件?IOW:如何反转以下内容以从PictureBox.Image获取字节数组?
pictureBox.Image = new Bitmap(new MemoryStream(jpgImage));
Run Code Online (Sandbox Code Playgroud) 我似乎无法弄清楚这一点.我有2个具有不同特征的java类,每个类调用BitmapFactory.decodeResource来获取相同的图像资源,一个返回位图而另一个返回null.两个类都在同一个包中.
这是有效的类,它调用BitmapFactory.decodeResource返回位图.我只包含相关代码.
package advoworks.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainScreen extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainScreen.class.getSimpleName();
public MainScreen(Context context) {
super(context);
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
//adding the callback (this) to the surface holder to intercept events;
getHolder().addCallback(this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是不起作用的类.BitmapFactory.decodeResource在调试中返回NULL.我只包括我觉得相关的代码.
package advoworks.test;
import android.content.res.Resources;
import android.graphics.Bitmap; …Run Code Online (Sandbox Code Playgroud) 我有一个带有Master-Details视图的应用程序.当您从"主"列表中选择一个项目时,它会使用一些图像(通过RenderTargetBitmap创建)填充"详细信息"区域.
每次我从列表中选择一个不同的主项目时,我的应用程序使用的GDI句柄数量(在Process Explorer中报告)会上升 - 并且最终会在使用的10,000个GDI句柄中翻倒(或有时会锁定).
我对如何解决这个问题感到茫然,所以对于我做错了什么建议(或者只是提供如何获取更多信息的建议)将不胜感激.
我在一个名为"DoesThisLeak"的新WPF应用程序(.NET 4.0)中将我的应用程序简化为以下内容:
在MainWindow.xaml.cs中
public partial class MainWindow : Window
{
public MainWindow()
{
ViewModel = new MasterViewModel();
InitializeComponent();
}
public MasterViewModel ViewModel { get; set; }
}
public class MasterViewModel : INotifyPropertyChanged
{
private MasterItem selectedMasterItem;
public IEnumerable<MasterItem> MasterItems
{
get
{
for (int i = 0; i < 100; i++)
{
yield return new MasterItem(i);
}
}
}
public MasterItem SelectedMasterItem
{
get { return selectedMasterItem; }
set
{
if (selectedMasterItem != value) …Run Code Online (Sandbox Code Playgroud) 我有Image Gridview,我想逐个添加图像到gridview选择从相机中捕获的图库或图像.
我是所有内存管理主题的新手,所以有很多我不理解的东西.
我正在尝试在我的应用程序中缓存图像,但我的内存消耗有问题:
所有的Bitmap Chaching代码都是从这里复制粘贴的:http://developer.android.com/training/displaying-bitmaps/index.html
我调试了代码并在eclipse的DDMS视图中检查了堆大小,在这些代码行之后大约有15mb的跳转:
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
Run Code Online (Sandbox Code Playgroud)
在"decodeSampledBitmapFromResource"方法中.
图像是1024x800,75kb jpg文件.根据我在互联网上已经看到的内容,这个图像应该占用的内存量大约是1024*800*4(每像素字节数)= 3.125mb
关于这个主题的所有线程都没有说明为什么它会占用更多的内存.有没有办法用合理数量的内存缓存一个图像?
编辑
我尝试使用@ ArshadParwez下面的答案中建议的decodeFile方法.使用此方法,在BitmapFactory.decodeStream方法之后,内存仅增加了3.5mb - 问题解决了,但是我想直接从资源缓存位图.
我注意到在decodeResource方法中有2个内存"跳转" - 一个大约3.5mb - 这是合理的,另一个奇怪的是14mb.那些14mb用于什么,为什么会发生这种情况?
如何使用Glide库将Bitmap加载到我的ImageView中?我想用文本创建自定义图像,并使用Glide将其加载到imageview中.
这是我用文本创建自定义位图的方法
public Bitmap imageWithText(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.BLACK);
tv.setTypeface(null, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
tv.setPadding(0, 25, 0, 0);
Bitmap testB = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 100, 100);
tv.draw(c);
return testB;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用滑动加载此位图时,我收到错误
Glide.with(getContext()).load(imageWithText("Random text")).into(holder.imgPhoto);
Run Code Online (Sandbox Code Playgroud) 注意:我知道有很多关于这个的问题和存储库,但似乎没有一个适合我试图实现的目标.
给定任何宽高比的位图,我希望将其设置为ImageView的内容(仅使用drawable,而不扩展ImageView),以便内容将被中心裁剪,但仍为圆形.
所有这一切,内存使用量最少,因为有时图像可能非常大.我不想为此创建一个全新的Bitmap.内容已经存在......
我发现的所有解决方案都没有我写过的东西:有些不是中心裁剪,有些人认为图像是方形的,有些是从给定的位图创建一个新的位图......
除了尝试各种存储库之外,我已经尝试过本教程,并尝试针对非方形宽高比进行修复,但我失败了.
这是它的代码,以防网站关闭:
public class RoundImage extends Drawable {
private final Bitmap mBitmap;
private final Paint mPaint;
private final RectF mRectF;
private final int mBitmapWidth;
private final int mBitmapHeight;
public RoundImage(Bitmap bitmap) {
mBitmap = bitmap;
mRectF = new RectF();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas) {
canvas.drawOval(mRectF, mPaint);
} …Run Code Online (Sandbox Code Playgroud) 我正在进行一个屏幕共享项目,我不断收到一小块图像Socket,需要在我拥有的某个初始dekstop位图上更新它们.
基本上我不断地从插座(其被存储为JPEG图像数据)读出的数据,使用Image.FromStream()以检索图像和复制收到块象素到全初级位图(在特定的位置X和Y其我也从插座获得) -这就是如何初始图像得到更新.但接下来是我需要在Picturebox
我处理Paint事件并再次重新绘制它的部分 - 整个初始图像,这是非常大的(在我的情况下是1920X1080).
这是我的代码:
private void MainScreenThread()
{
ReadData();//reading data from socket.
initial = bufferToJpeg();//first intial full screen image.
pictureBox1.Paint += pictureBox1_Paint;//activating the paint event.
while (true)
{
int pos = ReadData();
x = BlockX();//where to draw :X
y = BlockY();//where to draw :Y
Bitmap block = bufferToJpeg();//constantly reciving blocks.
Draw(block, new Point(x, y));//applying the changes-drawing the block on the big initial image.using native memcpy.
this.Invoke(new …Run Code Online (Sandbox Code Playgroud) 我试图拍照然后使用照片.这就是我做的.
我的设备是Nexus 6P(Android 7.1.1).
首先,我创建了一个Uri:
Uri mPicPath = UriUtil.fromFile(this, UriUtil.createTmpFileForPic());
//Uri mPicPath = UriUtil.fromFile(this, UriUtil.createFileForPic());
Run Code Online (Sandbox Code Playgroud)
然后,我开始Intent:
Intent intent = ActivityUtils.getTakePicIntent(mPicPath);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RequestCode.TAKE_PIC);
}
Run Code Online (Sandbox Code Playgroud)
最后,我处理了这个Uri问题onActivityResult:
if (requestCode == RequestCode.TAKE_PIC) {
if (resultCode == RESULT_OK && mPicPath != null) {
Bitmap requireBitmap = BitmapFactory.decodeFile(mPicPath.getPath());
//path is like this: /Download/Android/data/{@applicationId}/files/Pictures/JPEG_20170216_173121268719051242.jpg
requireBitmap.recycle();//Here NPE was thrown.
}
}
Run Code Online (Sandbox Code Playgroud)
在此期间,以下是UriUtil:
public class UriUtil {
public static File createFileForPic() throws …Run Code Online (Sandbox Code Playgroud) android bitmap android-camera-intent android-fileprovider android-7.0-nougat
bitmap ×10
android ×7
c# ×3
java ×2
aspect-ratio ×1
caching ×1
canvas ×1
gdi ×1
geometry ×1
graphics ×1
gridview ×1
performance ×1
picturebox ×1
wpf ×1