Android中ImageView属性的使用(四)

来源:互联网 发布:apache maven是什么 编辑:程序博客网 时间:2024/06/11 20:48
package com.itarchy.demo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;/** *  * @android:scaleType="fitCenter" * @属性表示图片在ImageieView中的缩放形式,该属性主要有以下几个常用的值 * @fitStart:等比例缩放,靠左上显示 * @fitEnd:等比例缩放,靠右下显示 * @fitCenter :等比例缩放,长边匹配,居中显示 * @center:不缩放,居中显示,按照ImageView的大小来显示 * @centerInside:如果图大,则等比例缩放,居中显示;如果图小,则不缩放,直接居中显示 * @centerCrop:等比例缩放,短边匹配,长多多余截掉居中显示 * @fitXY:分别按照长边和短边缩放填满ImageView * @matrix / matrix 用矩阵来绘制 * @android:src="@drawable/ic_launcher" * @android:background=”@null” 背景色 * @同时设置的时候 图片上方,背景色下方 * @应用:ImageView上添加点击事件,点击图片进行图片切换 *  *  */public class MainActivity extends Activity implements OnClickListener {private Button btn_fitStart;private Button btn_fitEnd;private Button btn_fitCenter;private Button btn_center;private Button btn_centerInside;private Button btn_centerCrop;private Button btn_fitXY;private Button btn_matrix;private Button btn_defualt;private ImageView btnImg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setListenr();}private void initView() {btn_fitStart = (Button) this.findViewById(R.id.fitStart);btn_fitEnd = (Button) this.findViewById(R.id.fitEnd);btn_fitCenter = (Button) this.findViewById(R.id.fitCenter);btn_center = (Button) this.findViewById(R.id.center);btn_centerInside = (Button) this.findViewById(R.id.centerInside);btn_centerCrop = (Button) this.findViewById(R.id.centerCrop);btn_fitXY = (Button) this.findViewById(R.id.fitXY);btn_matrix = (Button) this.findViewById(R.id.matrix);btn_defualt = (Button) this.findViewById(R.id.btn_default);}private void setListenr() {btn_fitStart.setOnClickListener(this);btn_fitEnd.setOnClickListener(this);btn_fitCenter.setOnClickListener(this);btn_center.setOnClickListener(this);btn_centerInside.setOnClickListener(this);btn_centerCrop.setOnClickListener(this);btn_fitXY.setOnClickListener(this);btn_matrix.setOnClickListener(this);btn_defualt.setOnClickListener(this);}@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, ImageViewActivity.class);switch (v.getId()) {case R.id.fitStart:intent.putExtra("ScaleType", "fitStart");break;case R.id.fitEnd:intent.putExtra("ScaleType", "fitEnd");break;case R.id.fitCenter:intent.putExtra("ScaleType", "fitCenter");break;case R.id.center:intent.putExtra("ScaleType", "center");break;case R.id.centerInside:intent.putExtra("ScaleType", "centerInside");break;case R.id.centerCrop:intent.putExtra("ScaleType", "centerCrop");break;case R.id.fitXY:intent.putExtra("ScaleType", "fitXY");break;case R.id.matrix:intent.putExtra("ScaleType", "matrix");break;case R.id.btn_default:intent.putExtra("ScaleType", "default");break;}startActivity(intent);}}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/title"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:gravity="center_horizontal"        android:text="ImageView属性的使用" />    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/title"        android:layout_marginTop="25dip"        android:orientation="horizontal" >        <Button            android:id="@+id/fitStart"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="fitStart" />        <Button            android:id="@+id/fitEnd"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="fitEnd" />    </LinearLayout>    <LinearLayout        android:id="@+id/linearLayout2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/linearLayout1"        android:orientation="horizontal" >        <Button            android:id="@+id/fitCenter "            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="fitCenter " />        <Button            android:id="@+id/center"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="center" />    </LinearLayout>    <LinearLayout        android:id="@+id/linearLayout3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/linearLayout2"        android:orientation="horizontal" >        <Button            android:id="@+id/centerInside "            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="centerInside " />        <Button            android:id="@+id/centerCrop"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="centerCrop" />    </LinearLayout>    <LinearLayout        android:id="@+id/linearLayout4"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/linearLayout3"        android:orientation="horizontal" >        <Button            android:id="@+id/fitXY "            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="fitXY " />        <Button            android:id="@+id/matrix "            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="matrix" />    </LinearLayout>         <Button            android:id="@+id/btn_default"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/linearLayout4"            android:text="默认效果" /></RelativeLayout>

package com.itarchy.demo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.ImageView;/** * @根据MainActivity界面点击不同的Button,传过来的字符串ScaleType来执行不同的if语句 * @ImageView.setScaleType()表述设置类型 *  */public class ImageViewActivity extends Activity {private ImageView img;private String type;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.imageview_main);img = (ImageView) this.findViewById(R.id.img);Intent intent = this.getIntent();type = intent.getStringExtra("ScaleType");if ("fitStart".equals(type)) {img.setScaleType(ImageView.ScaleType.FIT_START);} else if ("fitEnd".equals(type)) {img.setScaleType(ImageView.ScaleType.FIT_END);} else if ("fitCenter".equals(type)) {img.setScaleType(ImageView.ScaleType.FIT_CENTER);} else if ("center".equals(type)) {img.setScaleType(ImageView.ScaleType.CENTER);} else if ("centerInside".equals(type)) {img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);} else if ("centerCrop".equals(type)) {img.setScaleType(ImageView.ScaleType.CENTER_CROP);} else if ("fitXY".equals(type)) {img.setScaleType(ImageView.ScaleType.FIT_XY);} else if ("matrix".equals(type)) {img.setScaleType(ImageView.ScaleType.MATRIX);}}}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <!-- android:scaleType="" -->    <!--    android:layout_width="100dip"    android:layout_height="100dip"    -->    <ImageView        android:id="@+id/img"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:src="@drawable/image" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itarchy.demo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.itarchy.demo.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="com.itarchy.demo.ImageViewActivity" />    </application></manifest>


0 0
原创粉丝点击