我有一个ImageView
s单元格的TableLayout 。我想做的是通过增加ImageView
表格中s的大小来创建缩放效果。这对于缩小效果非常好(图片使用调整大小ImageView
),但是,问题在于放大。将放大时图片不会调整大小ImageView
。
这是一个例子:
我希望图像的大小与ImageView的大小相同(每个黑色正方形是一个ImageView)
这是我用于缩放的代码(有关更好实现的任何提示将不胜感激,不希望使用捏缩放)。
zoomIndex ranges from -2, 2. (only allow them to zoom twice in each direction)
zoomInThreshold = 2, zoomOutThreshold = -2.
defaultSize = 50, zoomIncrement = 15;
if(zoomIndex < zoomInThreshold)
defaultSize += zoomIncrement;
for(int i = 0; i < table.getChildCount(); ++i)
{
TableRow row = (TableRow)table.getChildAt(i);
for(int r = 0; r < row.getChildCount(); ++r)
{
ImageView img = (ImageView)row.getChildAt(r);
if(zoomIndex < zoomInThreshold)
{
TableRow.LayoutParams imgLayoutParams = new TableRow.LayoutParams(
defaultSize,
defaultSize
);
img.setLayoutParams(imgLayoutParams);
if(img.getDrawable() != null)
{
img.setAdjustViewBounds(true);
img.setMaxHeight(defaultSize);
img.setMaxWidth(defaultSize);
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
}
}
}
if(zoomIndex < zoomInThreshold)
zoomIndex++;
找出问题,仅当我从xml文件加载图形时才会发生此问题。我要做的就是在加载工程图时添加此行
img.setAdjustViewBounds(true);
这是它适合的方式:
if(drawableID != -1)
{
Bitmap pic = BitmapFactory.decodeResource(getResources(), drawableID);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(pic, 50, 50, true);
im.setImageBitmap(resizedBitmap);
ic.setImage(im);
im.setTag(ic);
im.setAdjustViewBounds(true);
//rotate the icon
im.setScaleType(ImageView.ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.set(im.getImageMatrix());
matrix.postRotate(rotAngle, pic.getWidth() / 2, pic.getHeight() / 2);
im.setImageMatrix(matrix);
im.setOnLongClickListener(longListen);//only set this listener if there is something there
}
我只img.setAdjustViewBounds(true);
按了我的缩放按钮。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句