2016-04-19 17 views
0

私の問題を助けてください、少なくとも私に助言を与えてください。 ImageViewにオーバーレイされたTextViewがあります。ユーザーは画像上の任意の位置にテキストをドラッグできます。その後、選択した位置にテキストでイメージを保存します。Canvas.drawTextを使用してBitmapの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のすべての問題は、座標は常に間違っています。私はXとYの座標を見つける2つの関数を試しています。

まず:この機能を使用して

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; 
} 

、より良い1は、

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}; 
} 

あなたに何かアドバイスを正確Xを与えますか?キャンバスがテキストを描画するとき、Yは常に希望の位置よりも高くなります。 AsyncTaskで

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); 

以降:ここ

はCanvas.drawTextのためにデータを保存するために私のコードです

 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

check [this](http://pastebin.com/4h6WvGYj)コード、画像の左上隅をクリックし、右下のものをクリックしてくださいlogcatを見てください – pskink

答えて

1

TypedValue.applyDimension方法を使用して、デバイスの画面メトリックスに高さの値をマッピングしてみ

関連する問題