android多点触摸缩放图片
-
- /**
- * 自定义触摸事件
- * @author tian
- *
- */
- rivate class TouchListener implements OnTouchListener{
- private PointF startPoint = new PointF();
- private Matrix matrix = new Matrix();
- private Matrix currentMaritx = new Matrix();
-
- private int mode = 0;//用于标记模式
- private static final int DRAG = 1;//拖动
- private static final int ZOOM = 2;//放大
- private float startDis = 0;
- private PointF midPoint;//中心点
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- switch (event.getAction() & MotionEvent.ACTION_MASK)
- {
- case MotionEvent.ACTION_DOWN:
- mode = DRAG;
- currentMaritx.set(imageView.getImageMatrix());//记录ImageView当期的移动位置
- startPoint.set(event.getX(),event.getY());//开始点
- break;
- case MotionEvent.ACTION_MOVE://移动事件
- if (mode == DRAG) {//图片拖动事件
- float dx = event.getX() - startPoint.x;//x轴移动距离
- float dy = event.getY() - startPoint.y;
- matrix.set(currentMaritx);//在当前的位置基础上移动
- matrix.postTranslate(dx, dy);
- } else if(mode == ZOOM)
- {//图片放大事件
- float endDis = distance(event);//结束距离
- if(endDis > 10f){
- float scale = endDis / startDis;//放大倍数
- matrix.set(currentMaritx);
- matrix.postScale(scale, scale, midPoint.x, midPoint.y);
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- mode = 0;
- break;
- //有手指离开屏幕,但屏幕还有触点(手指)
- case MotionEvent.ACTION_POINTER_UP:
- mode = 0;
- break;
- //当屏幕上已经有触点(手指),再有一个手指压下屏幕
- case MotionEvent.ACTION_POINTER_DOWN:
- mode = ZOOM;
- startDis = distance(event);
- if(startDis > 10f){//避免手指上有两个茧
- midPoint = mid(event);
- currentMaritx.set(imageView.getImageMatrix());//记录当前的缩放倍数
- }
- break;
- }
-
- imageView.setImageMatrix(matrix);
- return true;
- }
- }
- /**
- * 两点之间的距离
- * @param event
- * @return
- */
- private static float distance(MotionEvent event){
- //两根线的距离
- float dx = event.getX(1) - event.getX(0);
- float dy = event.getY(1) - event.getY(0);
- return FloatMath.sqrt(dx*dx + dy*dy);
- }
- /**
- * 计算两点之间中心点的距离
- * @param event
- * @return
- */
- private static PointF mid(MotionEvent event){
- float midx = event.getX(1) + event.getX(0);
- float midy = event.getY(1) - event.getY(0);
- return new PointF(midx/2, midy/2);
- }
android保存图片到SD卡并以时间命名
- private void storeInSD(Bitmap bitmap1) {
- File file = new File(FILE_DIR);
- if (!file.exists()) {
- file.mkdir();
- }
- File imageFile = new File(file, FileUtils.getFileName() + ".png");
- try {
- imageFile.createNewFile();
- FileOutputStream fos = new FileOutputStream(imageFile);
- bitmap1.compress(CompressFormat.PNG, 50, fos);
-
- Toast.makeText(MainActivity.this, "保存成功!", 1);
-
- fos.flush();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "保存失败!", 1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "保存失败!", 1);
- }
- }
- private void storeInSD(Bitmap bitmap1) {
- File file = new File(FILE_DIR);
- if (!file.exists()) {
- file.mkdir();
- }
- File imageFile = new File(file, FileUtils.getFileName() + ".png");
- try {
- imageFile.createNewFile();
- FileOutputStream fos = new FileOutputStream(imageFile);
- bitmap1.compress(CompressFormat.PNG, 50, fos);
- Toast.makeText(MainActivity.this, "保存成功!", 1);
- fos.flush();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "保存失败!", 1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "保存失败!", 1);
- }
- }
- public static String getCharacterAndNumber() {
- String rel="";
- SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
- Date curDate = new Date(System.currentTimeMillis());
- rel = formatter.format(curDate);
- return rel;
- }
- public static String getFileName() {
- // mu
- //String fileNameRandom = getCharacterAndNumber(8);
- String fileNameRandom = getCharacterAndNumber();
- return fileNameRandom;
- }
android实现自动关机代码
- private void shutdown() {
- try {
- Process process = Runtime.getRuntime().exec("su");
- DataOutputStream out = new DataOutputStream(
- process.getOutputStream());
- out.writeBytes("reboot -p\n");
- out.writeBytes("exit\n");
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
Note:手机必须获取Root权限!!!
- private void shutdown() {
- try {
- Process process = Runtime.getRuntime().exec("su");
- DataOutputStream out = new DataOutputStream(
- process.getOutputStream());
- out.writeBytes("reboot -p\n");
- out.writeBytes("exit\n");
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了
我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendants”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItemClick()方法了。
1.android:fastScrollEnabled="true"
2.Listview组件没有提供修改快速滑块的图像的API,因此不能直接修改,需要用java的反射技术修改
- listView1 = (ListView)findViewById(R.id.listView1);
- listView1.setFastScrollEnabled(true);
- //获取FastScroller对象
- try {
- Field field = AbsListView.class.getDeclaredField("mFastScroller");
- field.setAccessible(true);
- Object obj = field.get(listView1);
- //获取FastScroller.mThumbDrable变量的Field对象
- field = field.getType().getDeclaredField("mThumbDrawable");
- field.setAccessible(true);
- //获取FastScroller.mThumbDrable的值
- Drawable drawable = (Drawable)field.get(obj);
- drawable = getResources().getDrawable(R.drawable.ic_launcher);
- field.set(obj, drawable);
-
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
运行效果:
android TextView图文混写 嵌入表情
- private void setImageText()
- {
- //根据ID获取图像的Bitmap对象
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
- //创建imageSpan对象
- ImageSpan imageSpan = new ImageSpan(this, bitmap);
- //创建一个SpannableString对imageSpan进行封装
- SpannableString spannableString = new SpannableString("icon哇咔咔");
- //用ImageSpan对象替换icon
- spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv_hello.setText(spannableString);
- }
彻底退出android程序的方法
- package com.cqupt.util;
- import java.util.LinkedList;
- import java.util.List;
-
- import android.app.Activity;
- import android.app.Application;
-
- public class ExitApplication extends Application {
-
- private List<Activity> activityList = new LinkedList();
- private static ExitApplication instance;
-
- private ExitApplication()
- {
- }
- //单例模式中获取唯一的ExitApplication实例
- public static ExitApplication getInstance()
- {
- if(null == instance)
- {
- instance = new ExitApplication();
- }
- return instance;
-
- }
- //添加Activity到容器中
- public void addActivity(Activity activity)
- {
- activityList.add(activity);
- }
- //遍历所有Activity并finish
-
- public void exit()
- {
-
- for(Activity activity : activityList)
- {
- activity.finish();
- }
-
- System.exit(0);
-
- }
- }
在每个Activity的OnCreate方法里面写上
- ExitApplication.getInstance().addActivity(this);
在要结束程序的地方:
- if (!isExit) {
- Toast.makeText(MoreActivity.this, "再点一次退出程序", Toast.LENGTH_SHORT).show();
- isExit = true;
- }else{
- ExitApplication.getInstance().exit();
- }
转载自:
http://www.yoyong.com/archives/199
如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了
1.android:fastScrollEnabled="true"
2.Listview组件没有提供修改快速滑块的图像的API,因此不能直接修改,需要用java的反射技术修改
- listView1 = (ListView)findViewById(R.id.listView1);
- listView1.setFastScrollEnabled(true);
- //获取FastScroller对象
- try {
- Field field = AbsListView.class.getDeclaredField("mFastScroller");
- field.setAccessible(true);
- Object obj = field.get(listView1);
- //获取FastScroller.mThumbDrable变量的Field对象
- field = field.getType().getDeclaredField("mThumbDrawable");
- field.setAccessible(true);
- //获取FastScroller.mThumbDrable的值
- Drawable drawable = (Drawable)field.get(obj);
- drawable = getResources().getDrawable(R.drawable.ic_launcher);
- field.set(obj, drawable);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
运行效果:
android TextView图文混写 嵌入表情
- private void setImageText()
- {
- //根据ID获取图像的Bitmap对象
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
- //创建imageSpan对象
- ImageSpan imageSpan = new ImageSpan(this, bitmap);
- //创建一个SpannableString对imageSpan进行封装
- SpannableString spannableString = new SpannableString("icon哇咔咔");
- //用ImageSpan对象替换icon
- spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- tv_hello.setText(spannableString);
- }
- package com.cqupt.util;
- import java.util.LinkedList;
- import java.util.List;
- import android.app.Activity;
- import android.app.Application;
- public class ExitApplication extends Application {
- private List<Activity> activityList = new LinkedList();
- private static ExitApplication instance;
- private ExitApplication()
- {
- }
- //单例模式中获取唯一的ExitApplication实例
- public static ExitApplication getInstance()
- {
- if(null == instance)
- {
- instance = new ExitApplication();
- }
- return instance;
- }
- //添加Activity到容器中
- public void addActivity(Activity activity)
- {
- activityList.add(activity);
- }
- //遍历所有Activity并finish
- public void exit()
- {
- for(Activity activity : activityList)
- {
- activity.finish();
- }
- System.exit(0);
- }
- }
在每个Activity的OnCreate方法里面写上
- ExitApplication.getInstance().addActivity(this);
在要结束程序的地方:
- if (!isExit) {
- Toast.makeText(MoreActivity.this, "再点一次退出程序", Toast.LENGTH_SHORT).show();
- isExit = true;
- }else{
- ExitApplication.getInstance().exit();
- }
转载自: http://www.yoyong.com/archives/199