Android ImageView控件的运用

来源:互联网 发布:淘宝月销售怎么查看 编辑:程序博客网 时间:2024/05/19 20:18
 最近在完成Android开发课程的综合性实验中,翻阅了关于ImageView控件的许多资料,现在总结一下该控件一些相关的使用。

一、在创建方面
1、直接在XML中通过拖拉形式创建
打开 项目\ res\layout 下的xml文档,点击Graphical layout将会出现一个视图界面 点击 Images&Media ,将其中的ImageView拖到手机主屏幕即可。
2、在XML书写代码实现
 
打开 项目\ res\layout 下的xml文档,点击 文档名.XML 将会出现代码区,在其中加入(我采用的是LinearLayout布局
<ImageView
            android:id="@+id/imageView1"
            android:layout_width="209dp"
            android:layout_height="wrap_content" 
            android:layout_gravity="center"/> 
3、在 .java 文档中书写代码实现 
ImageView imageView = new ImageView(Game.this); 
ll.addView(imageView); 
其中Game是java文档名,ll 则是用来放置ImageView的
LinearLayout布局。

二、ImageView图片的放置
首先将你准备好的图片放在每个 
项目\ res\drawable 下。
1、直接在XML中通过设置放置
打开 项目\ res\layout 下的xml文档,点击Graphical layout鼠标右键ImageView控件,选择Edit Src... 选择所需图片即可。
2、在XML书写代码实现
 打开 项目\ res\layout 下的xml文档,点击 文档名.XML 将会出现代码区,在其中加入
android:src="@drawable/fish"
其中,fish是图片名
3、在 .java 文档中书写代码实现  
imageView.setImageDrawable(getResources().getDrawable(R.drawable.fish));//设置图片 
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.fish);
imageView.setImageBitmap(bmp); 

三、ImageView图片的放大与缩小
放大:
        //设置图片放大比例   
        double scale = 1.25;  
        //计算出这次要缩小的比例   
        scaleWidth = (float) (scaleWidth * scale); 
        scaleHeight = (float) (scaleHeight * scale);  
        //产生ReSize之后的bmp对象   
        Matrix matrix = new Matrix();  
        matrix.postScale(scaleWidth, scaleHeight);  
        Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
        //设置图片
        imageView.setImageBitmap(resizeBmp); 
缩小与放大原理一样,将scale赋值为0~1即可。

四、ImageView在屏幕中按照指定方向移动
由下往上移动:
    TranslateAnimation ani_0 = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,  
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -4.0f);
        ani_0.setInterpolator(new AccelerateDecelerateInterpolator());     
        ani_0.setDuration(2000);  
        ani_0.setFillEnabled(true);  
        ani_0.setFillAfter(true);
       ImageView.startAnimation(ani_0);


开发工具下载:http://developer.android.com/sdk/index.html?hl=sk
0 0
原创粉丝点击