`
whotodo
  • 浏览: 168149 次
文章分类
社区版块
存档分类
最新评论

android 中layout 的大小缩放

 
阅读更多

1.首相要创建一个activity 代码如下:

package com.wljie.layout.z;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ZlayoutTestActivity extends Activity {
	
	 public RelativeLayout layout_parent;  
     
	 public Button scale_btn;
	 
	 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scale_btn = (Button) findViewById(R.id.scale_btn);  
        scale_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 switch (v.getId()) {  
			        case R.id.scale_btn:  
			            displayPage();  
			            v.setEnabled(false);  
			            break;  
			        case R.id.dismiss_btn:  
			            dismissPage();  
			            break;  
			        }  
			}
		}); 
  
        layout_parent = (RelativeLayout) findViewById(R.id.layout_parent);  
    }
    
    View layout;  
    ImageView jobShadow;  
  
    public void displayPage() {  
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
        layout = inflater.inflate(R.layout.second, null);  
        layout.setId(Constant.KEY_LAYOUT_ID);  
        jobShadow = (ImageView) layout.findViewById(R.id.jobShadow);  
  
        Drawable ico = getResources().getDrawable(R.drawable.icon);  
        jobShadow.setBackgroundDrawable(ico);  
        ico.mutate().setAlpha(200);  
  
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.FILL_PARENT,  
                LinearLayout.LayoutParams.FILL_PARENT);  
        layout_parent.addView(layout, layoutParams);  
  
        findDialogView();  
  
        // 显示layout进行缩放动画效果  
        ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
                Constant.KEY_FIRSTPAGE);  
        scaleHelper.ScaleOutAnimation(layout);  
    }  
  
    public void removeLayout() {  
  
        layout_parent.removeView(layout_parent  
                .findViewById(Constant.KEY_LAYOUT_ID));  
    }  
  
    Button dismiss_btn;  
  
    public void findDialogView() {  
        dismiss_btn = (Button) findViewById(R.id.dismiss_btn);  
        dismiss_btn.setOnClickListener(new OnClickListener() {
			
				public void onClick(View v) {
					 switch (v.getId()) {  
				        case R.id.scale_btn:  
				            displayPage();  
				            v.setEnabled(false);  
				            break;  
				        case R.id.dismiss_btn:  
				            dismissPage();  
				            break;  
				        }  
				}
				
		})  ;
    }  
  
    public void dismissPage() {  
        ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
                Constant.KEY_SECONDPAGE);  
        scaleHelper.ScaleInAnimation(layout);  
        scale_btn.setEnabled(true);  
    }  
}

2.然后创建一个创建类并实现AnimationListener接口 这个接口大家不会陌生吧。是动画效果的接口。

package com.wljie.layout.z;

import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

public class DisplayNextView implements AnimationListener {

    Object obj;  
    
    // 动画监听器的构造函数  
    Activity ac;  
    int order;  
  
    public DisplayNextView(Activity ac, int order) {  
        this.ac = ac;  
        this.order = order;  
    }  
  
    public void onAnimationStart(Animation animation) {  
    }  
  
    public void onAnimationEnd(Animation animation) {  
        doSomethingOnEnd(order);  
    }  
  
    public void onAnimationRepeat(Animation animation) {  
    }  
  
    private final class SwapViews implements Runnable {  
        public void run() {  
            switch (order) {  
            case Constant.KEY_SECONDPAGE:  
                ((ZlayoutTestActivity) ac).removeLayout();  
                break;  
            }  
        }  
    }  
  
    public void doSomethingOnEnd(int _order) {  
        switch (_order) {  
          
        case Constant.KEY_SECONDPAGE:  
            ((ZlayoutTestActivity) ac).layout_parent.post(new SwapViews());  
            break;  
        }  
    }  
}  

3. 定义常量的类:

package com.wljie.layout.z;

public class Constant {
	
    public final static int KEY_FIRSTPAGE = 1;  
    
    public final static int KEY_SECONDPAGE = 2;  
      
    public final static int KEY_LAYOUT_ID = 3;  
    
}

4.将动画单独做一个类package com.wljie.layout.z;

import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

public class ScaleAnimationHelper {
    
    Context con;  
    int order;  
 
    public ScaleAnimationHelper(Context con, int order) {  
        this.con = con;  
        this.order = order;  
    }  
 
    DisplayNextView listener;  
    ScaleAnimation myAnimation_Scale;  
        //放大的类,不需要设置监听器  
    public void ScaleOutAnimation(View view) {  
        myAnimation_Scale = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1f,  
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
                0.5f);  
        myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
        AnimationSet aa = new AnimationSet(true);  
        aa.addAnimation(myAnimation_Scale);  
        aa.setDuration(500);  
 
        view.startAnimation(aa);  
    }  
 
    public void ScaleInAnimation(View view) {  
        listener = new DisplayNextView((ZlayoutTestActivity) con, order);  
        myAnimation_Scale = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,  
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
                0.5f);  
        myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
               //缩小Layout的类,在动画结束需要从父View移除它  
        myAnimation_Scale.setAnimationListener(listener);  
        AnimationSet aa = new AnimationSet(true);  
        aa.addAnimation(myAnimation_Scale);  
        aa.setDuration(500);  
 
        view.startAnimation(aa);  
    }  
}

最后还有两个xml :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent" android:id="@+id/layout_parent">  
    <Button android:text="Scale" android:id="@+id/scale_btn"  
        android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>  
</RelativeLayout>  


second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
    android:id="@+id/jobContent">  
    <ImageView android:layout_width="fill_parent"  
        android:layout_height="fill_parent" android:id="@+id/jobShadow" />  
    <Button android:layout_width="fill_parent" android:text="Dismiss"  
        android:layout_marginTop="20dp" android:layout_height="wrap_content"  
        android:src="@drawable/icon" android:layout_alignParentBottom="true"  
        android:id="@+id/dismiss_btn" />  
</RelativeLayout>  


本文转自:http://edison-cool911.iteye.com/blog/704812

分享到:
评论

相关推荐

    Android 缩放图片 缩小和放大图片.rar

    如果是第一次操作,就删除原来默认的ImageView ,如果不是第一次按,就删除上次放大缩小所产生的ImageView ,产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中,这里要注意,因为图片放到最大时放大按钮...

    ZoomControls 缩放

    如何使用ZoomControls控件实现布局/字体的缩放 一般应用开发过程中,会在...这样布局中的textview 大小改变,整个布局控件也可随之缩小 } 3:同样也可以通过 zoomControls.setOnZoomInClickListener//实现缩放功能

    Android实现控件的缩放移动功能

    上面的gif中,依次进行了拖动——&gt;触摸右上角放大,缩小——&gt;触摸上方与右测边缘——&gt;双指放大缩小。 2 使用步骤 2.1 布局。外层一个LinearLayout,里面一个自定义的控件DragScaleView,为了能够更清楚的看到控件的...

    Android 图片处理缩放功能

    PS:在开发中我们会遇到一些图片处理问题,比如说缓存图片了、限制图片大小了、查看图片了等。上一篇文章介绍了图片的全景效果查看,今天介绍一个图片缩放,我们如果有时间的话,可以自己写一个属于自己的库,里面会...

    Android代码-提供一个流畅的自动缩放文字大小的扩展EditText

    AutosizeEditText for Android is an extension of native EditText that offer a smooth auto scale text size. Latest Version How to use Configuring your project dependencies Add the library dependency ...

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

     android:layout_width="match_parent"  android:layout_height="match_parent"&gt;  &lt;android.support.v4.view.ViewPager  android:id="@ id/viewpager_default"  android:layout_width="match_parent"  ...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON...

    android开发实例大全_王东华

    实例137: 优化Android Layout 559 实例138: 优化Bitmap图片 560 第14章 综合实例--手机地图系统 563 实例139: 使用Google地图开发一个综合 地图系统 563 14.1 项目分析 563 14.1.1 规划UI界面 563 14.1.2 数据...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON...

    Google Android SDK开发范例大全的目录

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON...

    SuperButton-这真的可能是最好用的按钮了.zip

    drawable_bottom="@mipmap/icon_like"图标距文字距离app:drawable_padding="20dp"根据文字大小缩放图标,默认为true,当为false时显示原图标大小app:drawable_auto="true"按钮支持的所有属性&lt;?xml version="1.0...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    (3)Considering self-adaption of pages, the system combined the ideas of progressive enhancement and embracing flow layout, aiming at personalitites of mobile facilities viewports, and realized ...

    CoverLoading:ImageView 循环加载进度

    它还将针对不同的图像大小自动缩放。如何使用将此包含在 gradle 中 compile ' me.biubiubiu.coverloading:library:0.2.2 ' 在 xml 中添加视图 &lt; com .bettycc.coverloading.library.CoverView android : ...

    Google Android SDK 开发范例大全01

    4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON...

Global site tag (gtag.js) - Google Analytics