ImageView中的一些用法
来源:互联网 发布:如何测试网络丢包 编辑:程序博客网 时间:2024/06/06 04:23
这个demo实现了点击按钮 改变图片的透明度,对图片的局部放大。有一点bitmap位图的内容有些不太明白。后期会专门研究这个类。
MainActivity.java
package lzl.edu.com.imageviewtest;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class FirstActivity extends Activity implements OnClickListener{ Button increase,reduce,next; ImageView image1,image2; private int alpha=255; int images[] = {R.mipmap.my_hotel_a,R.mipmap.my_hotel_b,R.mipmap.my_hotel_c, R.mipmap.my_hotel_d,R.mipmap.my_hotel_e}; int currentImage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } void init(){ increase = (Button)findViewById(R.id.increase); reduce = (Button)findViewById(R.id.reduce); next = (Button)findViewById(R.id.next); image1 = (ImageView)findViewById(R.id.image1); image2 = (ImageView)findViewById(R.id.image2); increase.setOnClickListener(this); reduce.setOnClickListener(this); next.setOnClickListener(this); image1.setOnTouchListener(new touchImages()); } @Override public void onClick(View v) { if(v.getId() == R.id.increase){ alpha +=5; Log.i("增加",""+alpha); } if(v.getId() == R.id.reduce){ alpha -= 5; Log.i("减少",""+alpha); } if(v.getId()==R.id.next){ nextImageSource(); } if(alpha >255){ alpha = 255; } if(alpha <0){ alpha = 0; } //改变图片的透明度 image2.setAlpha(alpha); } //查看下一张图片 public void nextImageSource(){ if(currentImage >= images.length){ currentImage = -1; } BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable(); //如果图片还未回收,先强制回收该图片 if(!bitmapDrawable.getBitmap().isRecycled()){ bitmapDrawable.getBitmap().recycle(); } //改变ImageView显示的图片 image1.setImageBitmap(BitmapFactory.decodeResource(getResources(),images[currentImage])); currentImage++; } //接触到图片的位置,在下方放大 class touchImages implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { Log.i("viewid----",""+v.getId()); BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable(); //获取第一个图片显示框中的位图 Bitmap bitmap = bitmapDrawable.getBitmap(); //bitmap图片实际大小与一个ImageView的缩放比例 double scale = bitmap.getWidth() / 320.0; //获取需要显示的图片的开始点 int x = (int)(event.getX()*scale); int y = (int)(event.getY()*scale); if(x + 120 >=bitmap.getWidth()){ x = bitmap.getWidth() -120; } if(y + 120 >=bitmap.getHeight()){ y = bitmap.getHeight() -120; } image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120)); image2.setAlpha(alpha); return false; } }}activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/increase" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:text="增加"/> <Button android:id="@+id/reduce" 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> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/image1" android:layout_width="150dp" android:layout_height="150dp" android:src="@mipmap/my_hotel" android:scaleType="fitCenter" /> <ImageView android:id="@+id/image2" android:layout_width="300dp" android:layout_height="300dp" android:src="@mipmap/my_hotel" android:contentDescription="124" /> </LinearLayout> </LinearLayout> </RelativeLayout>
1 0
- ImageView中的一些用法
- ImageView 中的一些有用方法
- pMock 中的一些用法...
- pMock 中的一些用法...
- Python中的一些用法
- Calendar中的一些用法
- Android中的ImageView的一些有用的属性
- Extjs中的一些特殊用法
- ext中的一些用法心得
- Java中的一些String用法
- ImageView 用法总结
- Android ImageView 用法总结
- Imageview.setAdjustViewBounds用法
- Android ImageView用法
- ImageView: ScaleType用法总结
- ImageView属性scaleType用法
- UIScrollview的一些用法,解决里面ImageView只添加不释放的问题
- WEB标准开发中的一些基本用法
- 如何绘制虚线
- JDBC和JNDI区别,数据库连接区别
- Python学习笔记(一):Python中的slice
- 8.10~8.15周题库
- hdu1717小数化分数2
- ImageView中的一些用法
- Java序列化及反序列化 示例讲解
- Leetcode 58 Length of Last Word
- SPOJ COT2 树上的莫队算法,树上区间查询
- Java中Integer直接比较大小
- 菜鸟成长日记-coalesce函数&&collate&&memcache缓存
- myeclipse通过jdbc 连接mysql数据库
- 存储过程&&数据库主从同步
- 把EXCEL用程序导入到ORACLE中(SpringMVC+MyBatis)