我有一个用TableLayout填充的ImageView,我使用这些视图通过将图标拖放到ImageView上来绘制图表。
如何工作:我有一个名为IconContainer的类,它是通过tag()属性(ImageView.setTag())分配给ImageView的类,然后将类的每个属性作为节点存储在xml文件中。(标记是帮助我进行其他操作并识别ImageViews)
这就是xml结构的样子:
-<column columnID="1">
<drawableID>2130837525</drawableID>
<compTypeID>-1</compTypeID>
<componentID>-1</componentID>
<rotation>180.0</rotation>
</column>问题终于来了。当我从文件中加载xml时,我希望根据旋转字段中的值(例如180度)旋转ImageView。但是当我设置矩阵时,它会加载一个不可见的ImageView。图标根本没有显示,这是完全相同的旋转方法,我用它之前,ImageView被保存到文件,我不知道为什么。下面是我加载xml的代码,填充所有需要填充的内容并旋转我的ImageView
for (int r=0; r < rowCount; r++)
{
NodeList cList = rList.item(r).getChildNodes();
TableRow tr = new TableRow(this);
for (int c=0; c < columnCount; c++)
{
int drawableID = -1;
NodeList properties = cList.item(c).getChildNodes();
IconContainer ic = new IconContainer();
//get the drawable id
String stringID = properties.item(0).getTextContent();
drawableID = Integer.valueOf(stringID);
ic.setDrawableID(drawableID);
ic.setCompTypeID(Integer.parseInt(properties.item(1).getTextContent()));
ic.setComponentID(Integer.parseInt(properties.item(2).getTextContent()));
float rotAngle = Float.valueOf(properties.item(3).getTextContent());
ic.setAngle(rotAngle);
Log.i("customException", "angle from laoded xml: " + rotAngle);
//Create our imageview that will act as our cell
ImageView im = new ImageView (this);
im.setOnDragListener(dropListener);
im.setOnClickListener(singleClickListener);
im.setOnLongClickListener(longListen);
im.setMinimumHeight(50);
im.setMinimumWidth(50);
if(drawableID != -1)
{
Bitmap pic = BitmapFactory.decodeResource(getResources(), drawableID);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(pic, 50, 50, false);
im.setImageBitmap(resizedBitmap);
ic.setImage(im);
ic.setName(context.getResources().getString(drawableID));
im.setTag(ic);
//rotate the icon ----------------------------> this is where the problem lays. the icon never shows up(if I take this out, then the icon shows up)
im.setScaleType(ImageView.ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.set(im.getImageMatrix());
matrix.postRotate(rotAngle, im.getWidth()/2, im.getHeight()/2);
im.setImageMatrix(matrix);
}
tr.addView(im);
}
table.addView(tr);
}发布于 2014-06-11 19:02:37
弄明白了。正是因为这句话:
matrix.postRotate(rotAngle, im.getWidth()/2, im.getHeight()/2);我的im的高度和宽度似乎是0,因此图标没有出现。以下是行之有效的解决方案:
matrix.postRotate(rotAngle, resizedBitmap.getWidth() / 2, resizedBitmap.getHeight() / 2);最后,我使用了我创建的调整大小的位图,并使用它作为我的高度和宽度的图片。
https://stackoverflow.com/questions/24169021
复制相似问题