2011-05-11 10 views
0

아래 그림을보세요.
코드가 belove 인 sdcard에서 이미지를로드하고 있습니다.android - 오른쪽 비율로 ImageView에서 이미지를 설정하는 방법

ImageView를 자동으로 채우기 위해 사진을 가져올 수 없습니다. 배경을 녹색으로 만 설정하면 문제가 강조됩니다. 나는 많은 android를 시도했다 : ScaleType은 CENTRE와 FIT_XY와 같지만.

임 현재 사용 inSampleSize를 = 8 만 이미지 뷰가 자동으로 적절한 높이/너비 비율로하고 화면

또한 또 다른 한가지는, 당신은 참조 ExitText 및 버튼이 위에 배치를 채우기 위해 이미지 크기를 조정하는 방법 Imageview (나는 생각한다). 나는 그 (것)들을 아래에 있고 ImageView를 막지 않기를 바란다. 어쩌면 내가 안드로이드 레이아웃 기술을 오해.

enter image description here

DrawView.java

public class DrawView extends ImageView { 

private Context ctx; 
public DrawView(Context context, AttributeSet atts) { 
    super(context, atts); 
    this.ctx = context; 
} 
@Override 
protected void onDraw(Canvas canvas) { 
    String filePath = Environment.getExternalStorageDirectory().toString() + "/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg"; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 8; 

    Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);  

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    canvas.drawBitmap(bitmap, null, rect,null); 
} 

}

Main1.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<com.hasse.move.DrawView 
    android:id="@+id/mainView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:background="#00FF00" 
    android:adjustViewBounds="true" 
    /> 
    <RelativeLayout 
    android:id="@+id/InnerRelativeLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" > 
    <EditText 
    android:id="@+id/edittextaddtext" 
    android:layout_width="fill_parent" 
    android:layout_toLeftOf="@+id/btnsave" 
    android:layout_height="wrap_content" 
    android:text="wrap" 
    /> 
    <Button 
    android:id="@+id/btnsave" 
    android:layout_alignParentRight="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="save" 
    /> 
    </RelativeLayout> 
</RelativeLayout> 

주요 활동

public class Main extends Activity { 

    DrawView theImage; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // draw the view 
     setContentView(R.layout.main1); 

     theImage = (DrawView) findViewById(R.id.mainView); 
     //do stuff 
    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) 
    { 
    // Toast.makeText(this, "onConfigurationChanged",Toast.LENGTH_LONG).show(); 
     super.onConfigurationChanged(newConfig); 
    } 
} 
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

당신은 당신의 캔버스의 크기를 알아낼 필요 : 당신이로드 된 비트 맵의 ​​크기 대신 캔버스의 전체 크기로 대상 사각형을 사용하고 여기에

답변

2

. 그것을 위해 당신은 onSizeChanged를 사용할 수 있습니다 :

+0

감사합니다. 이미지는 완벽하지만 가로 모드에서는 늘어납니다. 비트 맵을 설정하기가 너무 힘들다. 나는 Android.gallery와 imageSwitcher를 알고있다. 모든 기분에 어떤 그림이라도 쉽게 표시 할 수있다. – Erik

+0

캔버스에 그리기 때문에 이미지의 비율과 위치에 따라 목적지 사각형의 크기를 스스로 계산해야한다. . 공식을 바로 얻으면 오리엔테이션 변경시 제대로 크기가 조정됩니다. – Lumis

관련 문제