2012-01-15 4 views
0

私は以前これに関連する質問をしましたが、私の目標が間違っていたと思います。アンドロイドで縮尺変更された画像のタッチ可能な領域を定義する

私が持っているもの:グラフィックを表示し、複数のタッチ可能な領域を画像内の長方形として定義するカスタムImageView。私の問題はスケーリングです。私はビットマップファイルの実際の解像度に基づいてイメージのタッチ可能な領域を定義したいが、その座標を変換して、矩形がスケーリングされたイメージの同じ領域をカバーするようにする。

これは私がこれまで持っているものです: ビューが作成されると、スケールの大き

protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
super.onLayout(changed, left, top, right, bottom); 
Drawable pic=this.getDrawable(); 
int realHeight= pic.getIntrinsicHeight(); 
int realWidth=pic.getIntrinsicWidth(); 
int scaledHeight=this.getHeight(); 
int scaleWidth=this.getWidth(); 
heightRatio=(float)realHeight/scaledHeight; 
widthRatio=(float)realWidth/scaleWidth; 
} 

に、実際の比率を計算する今、私は長方形(複数可)を定義する座標を取りたいですオリジナル(非スケール)画像 上の画像の同じ領域にその四角形(複数可)を描く - しかし、規模の会計処理:

protected void onDraw(Canvas canvas) { 
super.onDraw(canvas); 
Paint p=new Paint(); 
p.setStrokeWidth(1); 
p.setColor(Color.BLUE); 
for (HotSpot h: spots) 
{ 
//Each hotspot has a rectangle defined in reference to the actual size of the image 
Rect old=h.getRect(); 
float offsetLeft=old.left+(old.left*widthRatio); 
float offsetTop=old.top+(old.top*heightRatio); 
float offsetRight=old.right+(old.right*heightRatio); 
float offsetBottom=old.bottom+(old.bottom*widthRatio); 
RectF nRect=new RectF(offsetLeft,offsetTop,offsetRight,offsetBottom); 
canvas.drawRect(nRect,p); 

} 

結果は「ボールパークで」ですがなく、かなり正確。どんな助けもありがとうございます。

あなたはこのソリューション試すことができ

答えて

0

:あなたは長さを取得するので、

  • は密度による画像の高さ(または幅)
  • 分割高さ(または幅)を取得し、画面密度を取得しますインチ単位で
  • 座標を画像の有効サイズとは無関係の座標と長さで割ります。
  • 同じポイントを描画する必要がある場合最初の画像で :

    float density = getResources().getDisplayMetrics().density; 
    int width = getWidth(); 
    float inchesLength = width/density; 
    float scaledXCenter = xCenter/inchesLength; 
    

    異なるスケーリングされた画像は、

例(ただし、上記と同様の操作を使用し得る)第2の画像のインチの長さの最後の結果を乗算します別の縮尺の同じ画像:

float density = getResources().getDisplayMetrics().density; 
int width = getWidth(); 
float inchesLength = width/density; 
float restoredXCenter = scaledXCenter * inchesLength; 
関連する問題