2016-04-19 23 views
0

請幫我解決我的問題,或者至少給我建議。 我有覆蓋TextView的ImageView。用戶可以將文字拖動到圖像上的任何位置。之後,我想將文字保存在選定的位置。使用Canvas.drawText從位圖上的TextView錯誤的Y座標

佈局(TextView的動態添加到根元素):

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:gravity="center_horizontal|center_vertical"> 
<FrameLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/root" 
android:padding="0dp" 
android:layout_margin="0dp"> 
<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:adjustViewBounds="true" 
    android:scaleType="centerInside"/> 
</FrameLayout> 
</LinearLayout> 

Y中的所有問題的協調永遠是錯的。我嘗試2函數來查找X和Y座標。

第一:

public float[] getPosition(ImageView imageView, TextView textView) { 
Matrix matrix = new Matrix(); 
imageView.getImageMatrix().invert(matrix); 
float[] coord = {textView.getX(), textView.getY()}; 
matrix.mapPoints(coord); 

return coord; 
} 

和更好的一個,使用此功能給你準確的X.

private float[] getCoordinates(ImageView iv, Bitmap bm, float x, float y){ 
    float projectedX = (float) ((double)x * ((double)bm.getWidth()/(double)iv.getWidth())); 
    float projectedY = (float) ((double)y * ((double)bm.getHeight()/(double)iv.getHeight())); 

    return new float[]{projectedX, projectedY}; 
} 

有什麼建議?當Canvas繪製文本時,Y總是高於所需的位置。

這裏是我的代碼來保存數據Canvas.drawText:

Bundle bundle = new Bundle(); 
        TextView textView = (TextView)relativeLayout.getChildAt(i); 
        bundle.putString("text", ((TextView) relativeLayout.getChildAt(i)).getText().toString()); 
        bundle.putFloatArray("coord", getPosition(imageView,textView)); 
        bundle.putFloat("size", size*scale); 
        new AsyncDraw().execute(bundle); 
中的AsyncTask

而經過:

 Bundle bundle = bundles[0]; 
     String text = bundle.getString("text"); 
     float[] coord = bundle.getFloatArray("coord"); 
     Canvas canvas = new Canvas(oriented); 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setTextSize(bundle.getFloat("size")); 
     canvas.drawText(text, coord[0],coord[1],paint); 
+0

方法#1有什麼問題?它應該工作(當然提供的textView.getX()和textView.getY()是正確的位置) – pskink

+0

不知道,第一個metod給我錯了Y和X座標 – evansir

+0

檢查[this](http://pastebin.com/4h6WvGYj)代碼,嘗試單擊圖像的左上角和右下角,觀看logcat – pskink

回答