我需要在列表页面中显示来自api的图像列表.为此,我使用了两种方法.
第一种方法:
通过将url转换为字节数组,然后将其转换为位图.请找到以下代码..
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
/* This approach slowdown the process*/
baf.append((byte) current);
}
byte[] img_ary= baf.toByteArray();
Run Code Online (Sandbox Code Playgroud)
将字节数组转换为位图:
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imgUrl);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Run Code Online (Sandbox Code Playgroud)
第二种方法:
基于高度和宽度的图像缩放
private static final String TAG_iamge = "Image";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static …Run Code Online (Sandbox Code Playgroud) 我有一个对象,其中包含一些字符串成员和一个位图成员.
该对象保存在一个带有String键和Object作为值的映射中.
我正在使用以下代码转换地图:
String json = new Gson().toJson(aMap);
Run Code Online (Sandbox Code Playgroud)
然后提取我使用的JSON映射(传递上面的JSON字符串):
Map<String, Object> aMap;
Gson gson = new Gson();
aMap = gson.fromJson(jsonString, new TypeToken<Map<String, Object>>() {}.getType());
Run Code Online (Sandbox Code Playgroud)
这部分有效,但存储在对象中的位图似乎已损坏?即当我尝试将位图应用于图像视图时,我得到一个例外.
我想我可能需要单独将位图转换为JSON的字符串,但希望有一个更简单的解决方案,任何想法?
谢谢.
我有一个整数形式的颜色,我希望该颜色是一个Bitmap形式.
有没有办法这样做?
我试过了
Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();
Run Code Online (Sandbox Code Playgroud)
但上面的代码给出了错误无法将ColorDrawable强制转换为BitmapDrawable
还有其他方法吗?
实际代码是
Palette.generateAsync(BitmapFactory.decodeFile(songArt),
new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(final Palette palette) {
if (Build.VERSION.SDK_INT >= 16) {
Drawable colorDrawable = new ColorDrawable(palette.getDarkVibrantColor(
getResources().getColor(R.color.noti_background)));
notificationCompat.bigContentView.setImageViewResource(R.id.noti_color_bg,
((BitmapDrawable) colorDrawable).getBitmap());
notificationManager.notify(NOTIFICATION_ID, notificationCompat);
}
}
}
);
Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要对象功能的应用程序.
我有一个覆盖图像B的图像A.用手指我需要擦除图像A以显示图像B.擦除必须按照手指流动的图像A
我正在尝试一些代码但仍然无法擦除图像A.这是我用于在图像上绘制一条线的代码(_imageToErase是图像A):
Canvas canvas;
Paint paint;
float downx = 0, downy = 0, upx = 0, upy = 0;
ImageView _imageToErase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.g_layout);
_imageToErase = (ImageView) findViewById(R.id.image_to_erase);
_imageToErase.setOnTouchListener(this);
}
@Override
public void onWindowFocusChanged(boolean hasFocus){
int width = _imageToErase.getWidth();
int height = _imageToErase.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(25);
paint.setAntiAlias(true);
_imageToErase.setImageBitmap(bitmap);
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch …Run Code Online (Sandbox Code Playgroud) 使用滑动android库我将图像作为位图(参见滑动文档),然后我尝试使用renderscript和ScriptIntrinsicBlur模糊位图,这是一个高斯模糊.(取自此stackoverflow帖子)
Glide.with(getApplicationContext())
.load(ImageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(300,200) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
RenderScript rs = RenderScript.create(mContext); // context = this. this referring to the activity
final Allocation input = Allocation.createFromBitmap( rs, resource, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
final Allocation output = Allocation.createTyped( rs, input.getType() );
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
script.setRadius(8f);
script.setInput(input);
script.forEach(output);
output.copyTo(resource);
mImageView.setImageBitmap(resource);
}
});
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢谢谢.:)
public void screenShot(string path)
{
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
bmpScreenshot.Save(path, ImageFormat.Png);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码捕获我的计算机屏幕.
但今天我发现有一个名为Bitmap.Dispose()的方法.
什么时候调用Dispose()有什么区别?代码运行至关重要吗?
我要求我需要创建一个自定义相机,并允许用户在捕获图像时放置徽标.徽标可以放大/缩小,并可在任何地方的摄像机视图中移动.我已经编写了以下代码来执行此操作,我能够成功放大/缩小并移动徽标图像,但是当我结合使用从相机拍摄的徽标和图片时,它没有正确组合.徽标图像放置在不同的位置,它的大小减少了.请有人帮助我,因为我已经卡在这里,无法找到问题所在.我还附上了手机中的截图供参考.请检查一下.
在点击捕获按钮之前,我已将徽标移动到左下角
点击捕获按钮后,两个图像组合如下.
public class CustomCamera extends Activity implements OnTouchListener,
SurfaceHolder.Callback {
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
private float d = 0f;
private float newRot = 0f;
private float[] lastEvent …Run Code Online (Sandbox Code Playgroud) 正如Git中心上的Fresco项目所说:"在Android 4.x及更低版本中,Fresco将图像放在Android内存的特殊区域.这可以让您的应用程序运行得更快 - 并且更容易遭受可怕的OutOfMemoryError."
从这个答案,我知道它使用ashmem来放置位图.
而我的问题是:为什么壁画剂量不能在Android 5.0或更高版本的ashmem中放置位图?Android是否会更改某些系统功能并将其禁用?
嗨,我试着编写一个截图的功能.我找到了一个代码,通过按钮clicklistner完美地完成它,但当我删除按钮clicklistner并尝试在oncreate中截取我获得的位图是空的.为什么会这样?
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="munch"
android:id="@+id/munchscreen"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="500dp"
android:id="@+id/screenshots"
/>
Run Code Online (Sandbox Code Playgroud)
活动:
public class MainActivity extends Activity {
LinearLayout L1;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
Button but = (Button) findViewById(R.id.munchscreen);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为具有内存限制的嵌入式应用程序开发固件.我有一组需要在收到时处理的命令.每个命令都属于不同的"桶",每个"桶"都有一系列有效的命令号.我创建了两个ENUM,如下所示,以实现此目的.
enum
{
BUCKET_1 = 0x100, // Range of 0x100 to 0x1FF
BUCKET_2 = 0x200, // Range of 0x200 to 0x2FF
BUCKET_3 = 0x300, // Range of 0x300 to 0x3FF
...
...
BUCKET_N = 0xN00 // Range of 0xN00 to 0xNFF
} cmd_buckets;
enum
{
//BUCKET_1 commands
CMD_BUCKET_1_START = BUCKET_1,
BUCKET_1_CMD_1,
BUCKET_1_CMD_2,
BUCKET_1_CMD_3,
BUCKET_1_CMD_4,
//Add new commands above this line
BUCKET_1_CMD_MAX,
//BUCKET_2 commands
CMD_BUCKET_2_START = BUCKET_2,
BUCKET_2_CMD_1,
BUCKET_2_CMD_2,
BUCKET_2_CMD_3,
//Add new commands above this line
BUCKET_2_CMD_MAX,
//BUCKET_3 commands
...
... …Run Code Online (Sandbox Code Playgroud) bitmap ×10
android ×8
imageview ×2
c ×1
c# ×1
canvas ×1
dispose ×1
embedded ×1
enums ×1
fresco ×1
imagehandler ×1
imageurl ×1
java ×1
json ×1
renderscript ×1
screenshot ×1
surfaceview ×1