跳到主要內容

Android ImageView ScaleType

大家好,我是奶綠茶
在 Android 裡的 ImageView 元件,可以放置圖片
其中可以設定 ScaleType
官方 API 說明

為了方便測試,在這先準備二張圖片
450x500和 500x450
筆者以 320x480 的 Emulator 來測試

Layout 的部份放上二個 RadioButton 用來切換二圖不同 Size 的圖片
再放上六個 Button 來顯示圖片同 Size 的圖片 再放上六個 Button 來顯示圖片
主程式
package milkmidi.tutorial.imagescaletype;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class ImageViewScaleTypeMainActivity extends Activity implements OnCheckedChangeListener {
	

	Drawable mCurrentDrawable;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        onCheckedChanged(null,R.id.radio_450x500);
    }
    @Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
    	switch (checkedId) {
			case R.id.radio_450x500:	
				mCurrentDrawable = getResources().getDrawable(R.drawable.a450x500); 
				break;
			case R.id.radio_500x450:	
				mCurrentDrawable = getResources().getDrawable(R.drawable.a500x450); 
				break;
		}		
	}    
    public void onClick(View v){
    	ScaleType scaleType = null;
    	switch (v.getId()) {
			case R.id.center_btn:	
				scaleType = ScaleType.CENTER;
				break;
			case R.id.center_crop_btn:
				scaleType = ScaleType.CENTER_CROP;
				break;
			case R.id.center_inside_btn:	
				scaleType = ScaleType.CENTER_INSIDE;
				break;
			case R.id.fit_center_btn:		
				scaleType = ScaleType.FIT_CENTER;
				break;
			case R.id.fit_end_btn:			
				scaleType = ScaleType.FIT_END;
				break;
			case R.id.fit_start_btn:		
				scaleType = ScaleType.FIT_START;
				break;
			case R.id.fit_xy_btn:			
				scaleType = ScaleType.FIT_XY;
				break;
			case R.id.matrix_btn:			
				scaleType = ScaleType.MATRIX;
				break;
		}    	
    	
    	ViewGroup view = (ViewGroup)getLayoutInflater().inflate(R.layout.image_view, null);
    	TextView tf = (TextView) view.findViewById(R.id.title_txt);
    	tf.setText(scaleType.toString());
    	
    	ImageView image = (ImageView)view.findViewById(R.id.image_view);
    	image.setImageDrawable(mCurrentDrawable);
    	image.setScaleType(scaleType);
    	PopupWindow pop = new PopupWindow(
    			view,
    			LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT, 
				true);
    	pop.setContentView(view);
    	pop.setBackgroundDrawable(new ColorDrawable(0x5500ff00));
    	pop.showAtLocation(findViewById(R.id.root), Gravity.TOP, 0, 0);
    	pop.update();
    }


	
}


CENTER  Center the image in the view, but perform no scaling.
圖中置中,但不縮放





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).
等比縮放,將超出範圍的切掉




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).
等比縮放,讓圖片縮至容器裡




FIT_CENTER  Scale the image using CENTER.
等比縮放,將圖片放在容器裡的正中間




FIT_END  Scale the image using END.
等比縮放,將圖片放在容器裡的下方




FIT_START  Scale the image using START.
等比縮放,將圖片放在容器裡的上方




FIT_XY  Scale the image using FILL.
整個稱滿容器,不會等比縮放





MATRIX  Scale using the image matrix when drawing.
研究中


轉載請註明出處
SourceCodeDownload

留言

這個網誌中的熱門文章

奶綠茶新書上市_密技公怖_程式碼下載

奶綠茶第一本 Flash 書籍上市啦 博客來書籍館 Flash 3D 特效宅急便 - 商業範例隨學隨 把我對 Flash 的研究與熱愛, 全寫在其中 本書的前幾章, 特別安排 ActionScript3.0 的許多範例 好讓你一步步瞭解 as2 和 as3 的差別 再安排製作動畫不可缺少的 Tweener 類別和一些常用的數學動畫公式 最後當然就來到本書的重點 PV3D 啦 從 PV3D 的原理, 基本架構一點一點的介紹 再透過許多實用的範例, 從中瞭解 PV3D 製作的小技巧 有任何書籍相關的問題, 也請不吝指教 感謝一路走來, 幫助過我的前輩們, 邦邦、Ticore、神魂、Maso、Erin 旗標出版社 小編 Bready 米蘭數位所有夥伴 米蘭數位ActionScript Team:Jason,Ash,Mark 本書大鋼: 1.ActionScript3.0概論 分析AS3的概念與語法, 從基本的滑鼠事件、外部載入、氣泡事件介紹起 適合熟悉AS2, 想轉戰AS3的人員。 2.程式碼製作動畫 使用簡單的數學公式, 套用到程式裡, 脫離死版的影格動畫。 3.製作動畫的好夥伴:Tweener 除了套用數學公式外, Tweener類別也能加速我們開發出更多的動畫效果, 不需辛辛苦苦的找公式。 4.自定類別 AS3的精神, 是在於強大的物件導向, 當然也要熟悉類別的寫法與使用。 5.Flash3D - Papervison3D 如果在Flash玩轉3D效果, PV3D是最好學, 最快上手的3D Flash Engine, 從簡單的3D概念, PV3D類別, 事件等。 6 漫天飛舞的Paper3DWorld效果 7 迷你旋轉木馬式秀圖效果 8 最經典! CoverFlow 圖像展示效果 9 天旋地轉 TiltViewer效果 10 FlatWall3D電視牆效果 11 經典旋轉木馬 Carousel3D 秀圖效果 12 光源材質展示與簡易型的方塊彈跳效果 13 載入外部3D模型與動畫 14 超立體旋轉九宮格 Box3DWorld 15 空間感與透明感兼具的 SpaceGallery 16 螺旋式 SpiralCarouse 影像展示 17 Flash10 3D功能 18 Flash效

超好用的無限免費網頁空間,無廣告,無流量限制

大家好,我是奶綠茶 今天來教大家如何申請一個無限免費速度又快的網頁空間 1 首先到  https://github.com/ 申請帳號(一直下一步,下一步,下一步) 2 到你的個人頁,切換上方的 tab 到 Repositories, 按下右鍵的 new 3 Repository name 一定要是這樣的格式 username.github.io 我的 github 網址是 github.com/milkmidi 那就要輸入 milkmidi.github.io 選擇 public, 這樣別人才看的到 private 有其他用途, 而且要付費才能使用 完成後按下 Create repository 5 安裝 SourceTree github 並不支援 FTP 或是網頁上傳,一定要透過指令碼 在這我們選用有圖型介面的軟體,方便大家學習 https://www.sourcetreeapp.com/ 下載並安裝 啟動後登入你的 github 帳號 6 clone 你的 github io 專案 右上角有個 Clone or download 點選後 複製 https 連結(不要選到 ssh ) 7 將 https 的連結貼到 SourceTree 8 上傳 html 到本機 github.io 資料夾,放一個 index.html 切換到 SourceTree, 這時會看到 Unstaged files 的欄位 選擇 Stage All 9 git 要求每次的 Commit, 都一定要打說明文字(好習慣) 輸入完成後,按下右邊的 Commit 10 發佈(Push),這樣就完成啦 可以到你的 http://milkmidi.github.io/ 去查看檔案有沒有出來 其他 Commit 可以想像是做一個記錄,你可以很多的 Commit 最後再一次 Push 上去 github 原本是給程式設計師用的版本控管服務 免費版提供無限空間讓你放檔案,但一定要是 public 想要有私有的 Project ,就只能付費 github.io 只能放靜態檔案,php, aspx 服務並不支援。 祝大家學習愉快 轉載請註明出處 奶綠的 github.io S

奶綠茶photoGalleryV3.5

奶小茶的photoGalleryV3.5原始檔 2007/12/30 更新小Bug,請重新下載 線上Demo: http://milkmidi.com/photogallery/galleryv3.html 1.支援SWFAddress,讓FLASH可以有上一頁下一頁和每張獨立網址的功能 2.圖片路徑圖說經由XML來設定 3.背景依據顯示圖片,自動變化成最接近的顏色 4.非商業用途,歡迎使用並修改(如果覺得我製作的好,也煩請加入我的logo) 5.可結合後端伺服器 00.art 相本集資料夾 com 奶綠茶的Class檔 swfaddress SWFAddress資料夾 swfobject swfobject資料夾 XML galleryV3.fla 原始檔 index.html 主檔html SWFAddress.as SWFaddressEvent.as Source Code Download 解壓密碼:回覆文章,即可看見Password