我正在为Android编写实时游戏,在查看SDK中提供的示例中的一些代码后,我很困惑何时应该在我的游戏中使用Bitmap或Drawable作为我的精灵.
有什么不同?哪一个更好(更快)的精灵,哪一个更好的静态背景?
mobile android android-2.2-froyo android-drawable android-bitmap
如何使用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) 我正在开发一个应用程序,我需要将图像转换为PDF.我尝试了一些东西,但问题是,PDF中的图像尺寸非常小.我需要解决方案来解决这个问题 此外,我正在寻找将多个图像转换为单个PDF文档.我将发布我尝试过的代码.
public void convertPDF(byte[] path)
{
String FILE = "mnt/sdcard/FirstPdf.pdf";
Document document=new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
try {
image=Image.getInstance(path);
document.add(new Paragraph("My Heading"));
document.add(image);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
当我将Bitmap转换为Byte数组时,我正在压缩图像,我猜,这就是原因.在不压缩图像的情况下,我无法将Bitmap转换为字节数组.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
byte[] byteArray=stream.toByteArray();
convertPDF(byteArray);
Run Code Online (Sandbox Code Playgroud)
这有什么解决方案吗?
更新
在这里,我已经实现了@Burak Cakir在答案中提出的答案.但现在我在PDF中获得更大的图像.为了更好地理解,请查看下面的图像.
我想简单地将位图从Android转换为OpenCV的Mat对象.Stack Overflow上经常讨论此主题.例如:
将Mat转换为Android的Bitmap Opencv ;
使用android相机捕获图像后将Bitmap转换为Mat ;
templateMatching mattoBitmap opencv for android
还有更多要找.我在这个答案中遵循了这个问题,但我仍然无法以正确的方式完成任务.
最小代码:
//First convert Bitmap to Mat
Mat ImageMat = new Mat ( image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMat);
//Do smth.
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);
//Then convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(), ImageMat.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(ImageMat, resultBitmap);
//Set member to the Result Bitmap. This member is displayed in an ImageView
mResult = resultBitmap;
Run Code Online (Sandbox Code Playgroud)
(注意:图像是提供给这行代码的位图)
错误:
08-07 15:13:59.188:E/AndroidRuntime(2115):致命异常:主要
08-07 15:13:59.188:E/AndroidRuntime(2115):java.lang.NoClassDefFoundError:org.opencv.core.Mat
但我的进口是:
import …Run Code Online (Sandbox Code Playgroud) 我有一个自定义视图,在onDraw()我,我试图执行位图屏蔽.我有一个squareBitmap(红色)填充整个视图,我有一个circleBitmap(蓝色)充当面具.我正在使用模式:PorterDuff.Mode.DST_IN.
我期待的结果是一个红色的圆圈.我明白了,但我也得到了一个黑色的不透明背景.我不想要这种不透明的背景,而应该是透明的.Figure 1是我得到Figure 2的结果,是我正在寻找的结果.
我的代码:( onDraw()为了这个问题,我把所有内容都移到了里面)
protected void onDraw(Canvas canvas) {
final int width = canvas.getWidth();
final int height = canvas.getHeight();
Bitmap circleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas circleCanvas = new Canvas(circleBitmap);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setColor(Color.BLUE);
circleCanvas.drawCircle(width / 2, height / 2, width / 2, p);
p.setColor(Color.RED);
Bitmap squareBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas squareCanvas = new Canvas(squareBitmap);
final Rect squareRect = new Rect(0, …Run Code Online (Sandbox Code Playgroud) 是的,我正在使用Picasso来加载位图.究其原因,我在我的适配器,而在另一个装载位图的一部分解码的URI,和我读到这里是
即使您的URL为空,也应始终致电Picasso.这样它就知道图像视图被回收了.
所以我试过这个....
Bitmap bitMap;
...
Picasso.with(getContext())
.load(bitMap)
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误
无法解析方法'load(android.graphics.Bitmap)'
我试图从a获取每个帧TextureView,不幸的是尝试:
textureView.getBitmap();
Run Code Online (Sandbox Code Playgroud)
性能降低的结果是获得位图的更快方法.使用NDK更好吗?
寻找实际的例子
在我的Android应用程序中,我上传并下载JPEG图像.对于某些图像BitmapFactory下采样下载的图像,我不明白为什么以及如何阻止它这样做.我上传和下载图像时记录Bitmap对象大小:
Uploading image with size 3840x2160
Opening received image of size 2601584B degradation 0 decoded 384x216
所以这里我的图像宽度和高度在解码后除以10 BitmapFactory.对于另一个图像,我从3480x4640到217x290,因此除以16.我首先想到它可能是由图像dpi元数据引起的,所以我尝试使用以下代码打开图像时强制它:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 1 << degradation;
opts.inScaled = false;
opts.inDensity = 1;
opts.inTargetDensity = 1;
opts.inScreenDensity = 1;
final Bitmap original = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
Log.d(Tags.IMAGES, "Opening received image of size " + data.length + " degradation " + degradation + " decoded " + original.getWidth() + "x" + original.getHeight());
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我给出的字节数(2MB)BitmapFactory与没有300x200px图像的大图像一致.所以我的上传代码和下载代码是正确的我在解码之前有正确的数据.
我强制所有密度以避免缩放并禁用缩放,我的样本大小为1,因为我的降级为0所以我不强制进行任何子采样.我仍然以一个非常小的图像结束.并 …
根据这个问题,我使用了一个自定义ImageGatter类来显示我使用Picasso从TextView中的服务器获取的图像
public class PicassoImageGetter implements Html.ImageGetter {
private TextView textView = null;
Context mContext;
public PicassoImageGetter() {
}
public PicassoImageGetter(TextView target, Context context) {
textView = target;
mContext = context;
}
@Override
public Drawable getDrawable(String source) {
BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();
Picasso.get().load(source).into(drawable);
return drawable;
}
private class BitmapDrawablePlaceHolder extends BitmapDrawable implements com.squareup.picasso.Target {
protected Drawable drawable;
@Override
public void draw(final Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
public void setDrawable(Drawable drawable) {
this.drawable …Run Code Online (Sandbox Code Playgroud) android android-image picasso android-bitmap android-internal-storage
您好我在使用Android Camera API编写相机应用程序!
它具有以下功能:
以下是用于拖放图像视图的代码:
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
xloc=X;
yloc=Y;
Toast.makeText(getContext(), "Location==="+Integer.toString(xloc)+"==="+Integer.toString(yloc), Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin …Run Code Online (Sandbox Code Playgroud) android ×10
android-bitmap ×10
picasso ×2
android-view ×1
bitmap ×1
imageview ×1
java ×1
mobile ×1
opencv ×1
overlay ×1
textureview ×1