顯示具有 ImageView 標籤的文章。 顯示所有文章
顯示具有 ImageView 標籤的文章。 顯示所有文章

2016年3月6日 星期日

[筆記] imageView.setScaleType

這陣子要寫一個放大圖片的功能,
使用ImageView來實作,
不過使用下面的程式碼,
圖片始終沒有達到放大的效果:


Bitmap image = null;
Bitmap resizedBitmap= null;
int w = imageW;
int h = imageH;

image =  BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
Matrix matrix = new Matrix();
matrix.postScale(2.0f, 2.0f);
resizedBitmap = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
//imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageBitmap(resizedBitmap);



xml大致如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scale_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >
    <ImageView android:id="@+id/spotlight"
            android:layout_width="320dp" 
            android:layout_height="180dp"
            android:scaleType="fitCenter" //這行也要拿掉,不必要.
            android:adjustViewBounds="true"
            android:layout_alignLeft="@id/mainView"

            />
</RelativeLayout>



最後發現需要加上:
imageView.setScaleType(ImageView.ScaleType.CENTER);
如此才會按圖片的size居中顯示,
當圖片長/寬超過View的長/寬,
截取圖片的居中部分顯示.

(圖晚點補上好了




其他相關的scale方式還有:
Enum Values
ImageView.ScaleType CENTER Center the image in the view, but perform no scaling. 
ImageView.ScaleType CENTER_CROP Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). 
ImageView.ScaleType CENTER_INSIDE Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). 
ImageView.ScaleType FIT_CENTER Scale the image using CENTER
ImageView.ScaleType FIT_END Scale the image using END
ImageView.ScaleType FIT_START Scale the image using START
ImageView.ScaleType FIT_XY Scale the image using FILL
ImageView.ScaleType MATRIX Scale using the image matrix when drawing. 


RefImageView.ScaleType





2015年9月21日 星期一

[Note] setAlpha VS setImageAlpha

前情提要:
app中的某顆按鈕alpha值只會是76或255,
alpha = 76看起來是半透明
alpha = 255看起來是原圖/不透明
(由於有版權的問題因此無法直接放圖)


但是在某機台上,
即使我們將他的alpha值設定為255,顯示的顏色仍為半透明;
本以為是framework層的問題,
(也許的確有相關,畫view的方法或先後順序造成的影響)
經過資料查詢後發現是調用的方法不佳。


====================================================


以下直接就程式碼做解釋,傳入的是OptionButton的型別:

原程式碼:
Drawable d = getBackground();
// loading the drawable here.
if (d == null) d = getDrawable();
if (d != null) d.setAlpha(isEnabled() ? 255 : 76);

先得到該按鈕的drawable後、再將drawable的alpha值做設定。

====================================================

修改後:
this.setImageAlpha(isEnabled() ? 255 : 76);

直接將OptionButton視為一個ImageView,
設定該View的透明度。
由於我們的OptionButton背景皆為透明的,
所以這邊不討論有背景的情形。
有背景的情形可參考:Android ImageView - setAlpha(float) vs setImageAlpha(int)

====================================================

原本的setAlpha可能被Google發現有某些問題,
可能是繪製效率或方法不佳,
所以在API 16後就被捨棄了。


花了蠻多時間去解決這個問題的,
特別的是,
app只有放在某機台上會有繪製錯誤的問題。


有興趣的朋友可以參考:
http://www.stormzhang.com/android/2014/03/16/best-practices-for-using-alpha