通过编程创建的Android可绘制对象是uggly(缩放)

通过编程创建的Android可绘制对象是uggly(缩放),android,drawable,scale,scaling,Android,Drawable,Scale,Scaling,我需要创建一个自定义的可绘制对象。我想这个自定义绘图显示一个梯度,将作为一个视图的背景应用的例子。为此,我重写了Drawable类的draw方法。我的CustomDrawable类的代码如下所示: 公共空白绘制(画布){ Rect clipBounds=canvas.getClipBounds(); 绘制矩形绘制=新绘制(); LinearGradient lg=新的LinearGradient(0,0,0,剪贴簿.height(),0xff838383,0xff242424,Shader.Ti

我需要创建一个自定义的可绘制对象。我想这个自定义绘图显示一个梯度,将作为一个视图的背景应用的例子。为此,我重写了Drawable类的draw方法。我的CustomDrawable类的代码如下所示:

公共空白绘制(画布){
Rect clipBounds=canvas.getClipBounds();
绘制矩形绘制=新绘制();
LinearGradient lg=新的LinearGradient(0,0,0,剪贴簿.height(),0xff838383,0xff242424,Shader.TileMode.CLAMP);
矩形涂料.固色剂(lg);
RectF rectangle=new RectF(0f,0.0f,clipBounds.width(),clipBounds.height());//以像素为单位
画布.drawRoundRect(矩形、0f、0f、矩形绘制);
}
当我在onCreate方法中将此可绘制对象指定为我的窗口的背景时,就会出现问题。渐变看起来没有问题,但是看起来它被缩放了,看起来真的很难看

奇怪的是,当我创建一个完全用XML绘制的等价图形,并将其直接指定为布局XML中的背景时,我没有这种糟糕的缩放效果(我只是添加了下面这行“window.setFormat(PixelFormat.rgba8888);”)

为什么当我以编程方式执行此操作时,没有相同的结果

每一个想法都是值得赞赏的!
感谢阅读;)

在我的项目中,我已经多次遇到同一个问题,每次由于缺乏时间(和懒惰),我都会对一个不太理想的解决方案感到满意。但最近我找到了一些时间来解决这个问题。这是我的解决方案,我希望它也能帮助你

Bitmap scaleDownLargeImageWithAspectRatio(Bitmap image)
            {
                int imaheVerticalAspectRatio,imageHorizontalAspectRatio;
                float bestFitScalingFactor=0;
                float percesionValue=(float) 0.2;

                //getAspect Ratio of Image
                int imageHeight=(int) (Math.ceil((double) image.getHeight()/100)*100);
                int imageWidth=(int) (Math.ceil((double) image.getWidth()/100)*100);
                int GCD=BigInteger.valueOf(imageHeight).gcd(BigInteger.valueOf(imageWidth)).intValue();
                imaheVerticalAspectRatio=imageHeight/GCD;
                imageHorizontalAspectRatio=imageWidth/GCD;
                Log.i("scaleDownLargeImageWIthAspectRatio","Image Dimensions(W:H): "+imageWidth+":"+imageHeight);
                Log.i("scaleDownLargeImageWIthAspectRatio","Image AspectRatio(W:H): "+imageHorizontalAspectRatio+":"+imaheVerticalAspectRatio);

                //getContainer Dimensions
                int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
                int displayHeight = getWindowManager().getDefaultDisplay().getHeight();
               //I wanted to show the image to fit the entire device, as a best case. So my ccontainer dimensions were displayWidth & displayHeight. For your case, you will need to fetch container dimensions at run time or you can pass static values to these two parameters 

                int leftMargin = 0;
                int rightMargin = 0;
                int topMargin = 0;
                int bottomMargin = 0;
                int containerWidth = displayWidth - (leftMargin + rightMargin);
                int containerHeight = displayHeight - (topMargin + bottomMargin);
                Log.i("scaleDownLargeImageWIthAspectRatio","Container dimensions(W:H): "+containerWidth+":"+containerHeight);

                //iterate to get bestFitScaleFactor per constraints
                while((imageHorizontalAspectRatio*bestFitScalingFactor <= containerWidth) && 
                        (imaheVerticalAspectRatio*bestFitScalingFactor<= containerHeight))
                {
                    bestFitScalingFactor+=percesionValue;
                }

                //return bestFit bitmap
                int bestFitHeight=(int) (imaheVerticalAspectRatio*bestFitScalingFactor);
                int bestFitWidth=(int) (imageHorizontalAspectRatio*bestFitScalingFactor);
                Log.i("scaleDownLargeImageWIthAspectRatio","bestFitScalingFactor: "+bestFitScalingFactor);
                Log.i("scaleDownLargeImageWIthAspectRatio","bestFitOutPutDimesions(W:H): "+bestFitWidth+":"+bestFitHeight);
                image=Bitmap.createScaledBitmap(image, bestFitWidth,bestFitHeight, true);

                //Position the bitmap centre of the container
                int leftPadding=(containerWidth-image.getWidth())/2;
                int topPadding=(containerHeight-image.getHeight())/2;
                Bitmap backDrop=Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.RGB_565);
                Canvas can = new Canvas(backDrop);
                can.drawBitmap(image, leftPadding, topPadding, null);

                return backDrop;
            }
使用Aspectratio(位图图像)向下缩放大图像
{
国际图像垂直光谱,图像水平投影;
浮点最佳拟合比例因子=0;
float perceionvalue=(float)0.2;
//获取图像的纵横比
int imageHeight=(int)(Math.ceil((double)image.getHeight()/100)*100);
int imageWidth=(int)(Math.ceil((double)image.getWidth()/100)*100);
int GCD=biginger.valueOf(imageHeight).GCD(biginger.valueOf(imageWidth)).intValue();
imaheVerticalAspectRatio=图像高度/GCD;
imageHorizontalAspectRatio=图像宽度/GCD;
Log.i(“使用Aspectratio缩放的向下大图像”,“图像尺寸(W:H):“+imageWidth+”:“+imageHeight”);
Log.i(“缩放后的大图像与AspectRatio”,“图像AspectRatio(W:H):“+imageHorizontalAspectRatio+”:“+ImageVerticalSpectratio”);
//getContainer维度
int displayWidth=getWindowManager().getDefaultDisplay().getWidth();
int displayHeight=getWindowManager().getDefaultDisplay().getHeight();
//我希望显示图像以适合整个设备,这是最好的情况。因此,我的ccontainer维度是displayWidth和displayHeight。对于您的情况,您需要在运行时获取容器维度,或者您可以将静态值传递给这两个参数
int leftMargin=0;
int rightMargin=0;
int-topMargin=0;
int-bottomMargin=0;
int containerWidth=显示宽度-(左边距+右边距);
int containerHeight=显示高度-(上边距+下边距);
Log.i(“按Aspectratio缩放的大尺寸图像”,“容器尺寸(宽:高):“+容器宽度+”:“+容器高度”);
//迭代以获取每个约束的bestFitScaleFactor

虽然((imageHorizontalAspectRatio*bestFitScalingFactor)您可以尝试:Paint rectanglePaint=new Paint(Paint.ANTI_ALIAS_FLAG);但我尝试了,结果不幸是相同的……我不知道为什么,所以我只是猜测……理论上的一个原因可能是可绘制对象的画布大小小于使用它的视图的实际大小。