[英]How to set Width fit to Screen in Landscape Orientation (Using TouchImageView) in android
我正在使用來自GitHub的MikeOrtiz TouchImageView
。 這是TouchImageView的鏈接
我試圖將寬度設置為“橫向”中的“屏幕方向”,這僅是一張圖片,以顯示我想做什么。
我要這個
這是一些代碼。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
viewWidth = MeasureSpec.getSize(widthMeasureSpec);
viewHeight = MeasureSpec.getSize(heightMeasureSpec);
//
// Rescales image on rotation
//
if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
|| viewWidth == 0 || viewHeight == 0)
return;
oldMeasuredHeight = viewHeight;
oldMeasuredWidth = viewWidth;
if (saveScale == 1) {
// Fit to screen.
float scale;
Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == 0
|| drawable.getIntrinsicHeight() == 0)
return;
int bmWidth = drawable.getIntrinsicWidth();
int bmHeight = drawable.getIntrinsicHeight();
Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
float scaleX = (float) viewWidth / (float) bmWidth;
float scaleY = (float) viewHeight / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale , scale );
// Center the image
float redundantYSpace = (float) viewHeight
- (scale * (float) bmHeight);
float redundantXSpace = (float) viewWidth
- (scale * (float) bmWidth);
redundantYSpace /= (float) 2;
redundantXSpace /= (float) 2;
matrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = viewWidth - 2 * redundantXSpace;
origHeight = viewHeight - 2 * redundantYSpace;
setImageMatrix(matrix);
}
fixTrans();
}
謝謝
您已經在res layout-land中創建了一個文件夾,然后在其中創建了一個具有相同名稱的xml文件,並在landsacpe模式下根據需要設置UI,它非常適合您
如果將home.xml放在layout-port文件夾中,則當設備以縱向方向放置時,它將使用文件:layout-port / home.xml。
如果將home.xml放在layout-land文件夾中,則當設備橫向放置時,它將使用文件:layout-land / home.xml。
對於不同的定向模式(例如縱向和橫向)的含義……我們使用兩個home.xml文件; 一個在布局端口,另一個在布局域。 另一方面,如果您希望兩者都使用相同的布局文件,則只需將home.xml放在布局文件夾中,然后將其從layout-land和layout-port中刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.