Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在不调整大小的情况下生成旋转位图_Android_Matrix_Bitmap_Rotation - Fatal编程技术网

Android 在不调整大小的情况下生成旋转位图

Android 在不调整大小的情况下生成旋转位图,android,matrix,bitmap,rotation,Android,Matrix,Bitmap,Rotation,我正试图从一张图像的20个副本中构建一个环,这是整个环的1/20切片。我生成的位图就是这个原始图像旋转到正确的度数。原始图像是一个130x130的正方形 生成旋转切片的代码如下所示: Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_green); FileOutputStream fos; for(int i = 0; i < 20; i++) {

我正试图从一张图像的20个副本中构建一个环,这是整个环的1/20切片。我生成的位图就是这个原始图像旋转到正确的度数。原始图像是一个130x130的正方形

生成旋转切片的代码如下所示:

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_green);
    FileOutputStream fos;
    for(int i = 0; i < 20; i++) {
        String idName = "batt_s_"+i;
        Matrix m = new Matrix();
        m.setRotate((i * 18)-8, bmp.getWidth()/2, bmp.getHeight()/2);

        Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
        try {
            fos = context.openFileOutput(idName+"_"+color+".png", Context.MODE_WORLD_READABLE);
            newBmp.compress(CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        m.reset();
        BattConfigureSmall.saveInitPref(context, true);
    }
Bitmap bmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.battery_绿色);
文件输出流;
对于(int i=0;i<20;i++){
字符串idName=“batt_s_”+i;
矩阵m=新矩阵();
m、 setRotate((i*18)-8,bmp.getWidth()/2,bmp.getHeight()/2);
Bitmap newBmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),m,true);
试一试{
fos=context.openFileOutput(idName+“u“+color+”.png”,context.MODE\u WORLD\u可读);
newBmp.compress(CompressFormat.PNG,100,fos);
fos.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
m、 重置();
battconfiguresall.saveInitPref(上下文,true);
}
这些生成的位图最终插入的ImageView的XML中都有scaleType=“center”。但是,生成的输出如下所示:

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_green);
    FileOutputStream fos;
    for(int i = 0; i < 20; i++) {
        String idName = "batt_s_"+i;
        Matrix m = new Matrix();
        m.setRotate((i * 18)-8, bmp.getWidth()/2, bmp.getHeight()/2);

        Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
        try {
            fos = context.openFileOutput(idName+"_"+color+".png", Context.MODE_WORLD_READABLE);
            newBmp.compress(CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        m.reset();
        BattConfigureSmall.saveInitPref(context, true);
    }


不完全是一个完美的戒指。切片本身,如果旋转正确,确实是一个完美的环,因为在API级别11及更高版本中,我在这些ImageView上使用了android:rotate XML属性,但我也需要支持API级别7-10,所以有人能给我一些建议吗?谢谢。

在这种情况下,不要将矩阵与
createBitmap
一起使用,我认为它会在图像大小方面做一些奇怪的事情。相反,创建一个新的
位图
画布
,然后使用矩阵绘制:

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.battery_green);
FileOutputStream fos;
Paint paint = new Paint();
paint.setAntiAlias(true);
Matrix m = new Matrix();

for(int i = 0; i < 20; i++) {
    String idName = "batt_s_"+i;
    m.setRotate((i * 18)-8, bmp.getWidth()/2, bmp.getHeight()/2);

    Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBmp);
    canvas.drawBitmap(bmp, m, paint);

    try {
        fos = context.openFileOutput(idName+"_"+color+".png", Context.MODE_WORLD_READABLE);
        newBmp.compress(CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    m.reset();
    BattConfigureSmall.saveInitPref(context, true);
}
Bitmap bmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.battery_绿色);
文件输出流;
油漆=新油漆();
paint.setAntiAlias(真);
矩阵m=新矩阵();
对于(int i=0;i<20;i++){
字符串idName=“batt_s_”+i;
m、 setRotate((i*18)-8,bmp.getWidth()/2,bmp.getHeight()/2);
Bitmap newBmp=Bitmap.createBitmap(bmp.getWidth()、bmp.getHeight()、Bitmap.Config.ARGB_8888);
Canvas Canvas=新画布(newBmp);
canvas.drawBitmap(bmp、m、paint);
试一试{
fos=context.openFileOutput(idName+“u“+color+”.png”,context.MODE\u WORLD\u可读);
newBmp.compress(CompressFormat.PNG,100,fos);
fos.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
m、 重置();
battconfiguresall.saveInitPref(上下文,true);
}

使用图像而不是绘制圆弧有什么特别的原因吗?这对我来说似乎更容易。我有一点辉光效果,我只是一般更舒适地处理图像文件,就创意控制而言。更不用说我已经在安卓3.x和更高版本上完成了整个工作。drawBitmap的第一个参数应该是newBmp而不是bmp吗?因为newBmp是压缩到文件中的位图。还有一个相关的问题,您的方法是否仍打算实际写入文件?不,
bmp
是正确的。您希望将旧位图(但经过
m
转换)绘制到新位图上。我添加的代码不会写入文件,而您已有的代码(
newBmp.compress
)已经这样做了。试一试,看看它是否对你有用(我试过了,它对我有用)。我会被诅咒的,看起来好多了。圆形本身就是完美的。唯一的问题是旋转后的图像看起来非常参差不齐。有没有办法使画布反别名?是的,您可以使用
Paint.setAntiAlias(true)
或者
Paint.setDither(true)
。我会更新的,你可以看看它是否有帮助。它似乎没有做任何事情,也没有设置抖动。它看起来还可以,比以前好得多,而且这种方法只适用于Android 2.3及以下的用户,所以除非你有任何其他防止锯齿的想法,否则我肯定会接受你的答案