前言
時間有限,追索生命的誠意和真實,比什么都重要。
ImageView組件
ImageView繼承自View組件,它的主要功能是用于顯示任何Drawable對象。
為了控制ImageView顯示的圖片,ImageView提供了下列方法。
- setImageBitmap(Bitmap bm):使用Bitmap位圖設置該ImageView顯示的圖片。
- setImageDrawable(Drawable drawable):使用Drawable對象設置該ImageView顯示的圖片。
- setImageResource(int resId):使用圖片資源ID設置該ImageView顯示的圖片。
- setImageURI(Uri uri):使用圖片的URI設置該ImageView顯示的圖片。
示例代碼
本例的圖片瀏覽器不僅可以改變圖片的透明度,還可以查看圖片指定區域的細節。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 定義三個按鈕 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度"
/>
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減小透明度"
/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一張"
/>
</LinearLayout>
<!-- 定義顯示圖片整體的ImageView -->
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="360dp"
android:src="@drawable/baxianhua"
android:scaleType="fitCenter"
/>
<!-- 定義顯示圖片局部細節的ImageView -->
<ImageView
android:id="@+id/image2"
android:layout_width="240dp"
android:layout_height="240dp"
android:background="#00f"
android:layout_margin="10dp"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
//定義一個訪問圖片的數組
int []images = new int[] {
R.drawable.baxianhua,
R.drawable.dengta,
R.drawable.juhua,
R.drawable.kaola,
R.drawable.qie,
R.drawable.shamo,
R.drawable.shuimo,
R.drawable.yujinx,
};
//定義默認顯示的圖片
int currentImg = 2;
//定義圖片的初始透明度
private int alpha = 255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button minus = (Button) findViewById(R.id.minus);
final Button plus = (Button) findViewById(R.id.plus);
final Button next = (Button) findViewById(R.id.next);
final ImageView image1 = (ImageView) findViewById(R.id.image1);
final ImageView image2 = (ImageView) findViewById(R.id.image2);
//定義查看下一張圖片按鈕的監聽器
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//控制ImageView顯示下一張圖片
image1.setImageResource(images[++currentImg % images.length]);
}
});
//定義改變圖片透明度的方法
View.OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
if(v == plus)
{
alpha -= 20;
}
if(v == minus)
{
alpha += 20;
}
if(alpha >= 255)
{
alpha = 255;
}
if(alpha <= 0)
{
alpha = 0;
}
//改變圖片透明度
image1.setImageAlpha(alpha);
}
};
//為兩個按鈕添加監聽器
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
image1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
//獲取第一個圖片顯示框中的位圖
Bitmap bitmap = bitmapDrawable.getBitmap();
//bitmap圖片實際大小與第一個ImageView的縮放比例
double scale = 1.0 * bitmap.getHeight()/image1.getHeight();
//獲取需要顯示的圖片的開始點
int x = (int)(event.getX() * scale);
int y = (int)(event.getY() * scale);
if(x + 240 > bitmap.getWidth())
{
x = bitmap.getWidth() - 240;
}
if(y + 240 > bitmap.getHeight())
{
y = bitmap.getHeight() - 240;
}
//顯示圖片的指定區域
image2.setImageBitmap(bitmap.createBitmap(bitmap, x, y, 240, 240));
image2.setImageAlpha(alpha);
return false;
}
});
}
}
效果
Screenshot_2017-10-18-16-07-20.png
提示
andriod:scaleType屬性,設置所顯示的圖片如何縮放或移動以適應ImageView的大小。常用屬性值如下。
matrix:使用matrix方式進行縮放。
fitXY:對圖片橫向、縱向獨立縮放,使得該圖片完全適應于該ImageView,圖片的縱橫比可能會改變。
fitStart:保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中,縮放完成后將該圖片放在ImageView的左上角。
fitCenter:保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中,縮放完成后將該圖片放在ImageView的中央。
fitEnd:保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageView中,縮放完成后將該圖片放在ImageView的右下角。
center:把圖片放在ImageView的中間,但不進行任何縮放。
ImageButton組件
ImageButton與Button的區別在于,Button生成的按鈕上顯示文字,而ImageButton上則顯示圖片。ImageButton派生了一個ZoomButton,可以代表“放大”、“縮小”兩個按鈕。
代碼示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 定義ZoomControls組件 -->
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
效果
Screenshot_1508379554.png
QuickContactBadge組件
QuickContactBadge繼承了ImageView,因為它的本質是圖片按鈕,也可以通過andriod:src屬性指定它顯示的圖片。額外增加的功能是該圖片可以關聯到手機中指定的聯系人,當用戶單擊該圖片時,系統將會打開相應聯系人的聯系方式界面。為了讓QuickContactBadge與特定聯系人關聯,可以調用如下方法。
assignContactFromEmail(String emailAddapp\src\main\ress,boolean lazyLookup):將該圖片關聯到指定E-mail地址對應的聯系人。
assignContactFromPhone(String phoneNumber,boolean lazyLookup):將該圖片關聯到指定電話號碼對應的聯系人。
assignContactUri(Uri contactUri):將該圖片關聯到特定Uri對應的聯系人。
代碼示例
contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<QuickContactBadge
android:id="@+id/badge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:text="halo"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
QuickContactBadge badge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
//獲取QuickContactBadge組件
badge = (QuickContactBadge) findViewById(R.id.badge);
//將QuickContactBadge組件與特定電話號碼對應的聯系人建立關聯
badge.assignContactFromPhone("010-666666", false);
}
}
效果
Screenshot_1508380462.png
使用QuickContactBadge打開的特定聯系人
Screenshot_1508380482.png