使用矩阵变换在视图中居中位图

时间:2014-10-02 20:35:01

标签: android android-canvas

它让我发疯,我尝试使用矩阵变换在视图中居中位图 它拒绝工作,我搜索了你提供postTranslate的数据类型,但无法找到任何关于它的信息...我天真地认为它是oiuxels,但显然不是这样的...... < / p>

这是我的代码:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    viewWidth = getMeasuredWidth();
    viewHeight = getMeasuredHeight();
    screenCenter = new Point(viewWidth / 2, viewHeight / 2);
    Log.d(TAG,"ViewWidth "+viewWidth+","+viewHeight+" cent "+ screenCenter);
    Bitmap map = hexgrid.getMap();
    Point moveTo = new Point(screenCenter.x - map.getWidth()/2,
                             screenCenter.y - map.getHeight()/2);
    Log.d(TAG,"map "+map.getWidth()+","+map.getHeight()+" trans "+moveTo);
    moveTo = new Point(323,140);
    mapTransform.postTranslate(moveTo.x,moveTo.y);

}

左上角的微积分给出了419,53,但光学正确的值 将是323,140,我没有看到我计算的和我测试的内容之间的关系...

所以我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,这很奇怪,如果有人对我的回答,将Matrix mapTransform移动到onDraw方法而不是onMeasure解决了问题......

现在我只消耗CPU周期,但至少它可以工作....