Android代码片段

收集代码片段,需要的时候粘贴复制即可,避免重复的无用功



1、自定义Toast

private WindowManager.LayoutParams params;
public void showToast(String location) {
		// View触摸监听器
		params = new WindowManager.LayoutParams();
		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
		// 没有焦点
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		// 不让锁屏
		params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
		// 半透明
		params.format = PixelFormat.TRANSLUCENT;
		// 电话优先级的UI
		params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
		view = LinearLayout.inflate(this, R.layout.address_show, null);
		int which = SystemConfig.getSystemConfig(AddressService.this).getAddressStyle();
		view.setBackgroundResource(call_image[which]);
		TextView text = (TextView) view.findViewById(R.id.address_show);
		text.setText(location);
		wm.addView(view, params);
     xml文件

<span style="white-space:pre">		</span><?xml version="1.0" encoding="utf-8"?>
<span style="white-space:pre">		</span><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  <span style="white-space:pre">		</span>android:layout_width="wrap_content"
    <span style="white-space:pre">		</span>android:layout_height="wrap_content"
    <span style="white-space:pre">		</span>android:gravity="center"
    <span style="white-space:pre">		</span>android:orientation="horizontal" >

    <span style="white-space:pre">		</span><TextView
        <span style="white-space:pre">		</span>android:id="@+id/address_show"
        <span style="white-space:pre">		</span>android:layout_width="wrap_content"
        <span style="white-space:pre">		</span>android:layout_height="wrap_content"
        <span style="white-space:pre">		</span>android:layout_marginTop="2dp"
        <span style="white-space:pre">		</span>android:drawableLeft="@android:drawable/ic_menu_call"
        <span style="white-space:pre">		</span>android:drawableStart="@android:drawable/ic_menu_call"
        <span style="white-space:pre">		</span>android:gravity="center"
        <span style="white-space:pre">		</span>android:textSize="20sp" />

<span style="white-space:pre">		</span></LinearLayout>



2、可移动的Toast窗体

<span style="white-space:pre">		</span>view.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					Log.i(TAG, "开始位置 :" + startX + "," + startY);
					break;
				case MotionEvent.ACTION_MOVE:
					int newX = (int) event.getRawX();
					int newY = (int) event.getRawY();
					Log.i(TAG, "新的位置 :" + newX + "," + newY);
					int dX = newX - startX;
					int dY = newY - startY;
					Log.i(TAG, "偏移量:" + dX + "," + dY);
					params.x = params.x + dX;
					params.y = params.y + dY;
					// 更新UI
					wm.updateViewLayout(view, params);
					// 重新初始化起点坐标的位置
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					break;
				default:
					break;
				}
				// 事件处理完成,停止父控件的响应事件
				return false;
			}
		});


3、跟随手指可移动的控件

<span style="white-space:pre">	</span>protected static final String TAG = "MainActivity";
	private int startX;
	private int startY;
	private Button view;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		view = (Button) findViewById(R.id.view);
		view.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("NewApi")
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					Log.i(TAG, "开始位置 :" + startX + "," + startY);
					break;
				case MotionEvent.ACTION_MOVE:
					int newX = (int) event.getRawX();
					int newY = (int) event.getRawY();
					Log.i(TAG, "新的位置 :" + newX + "," + newY);
					int dX = newX - startX;
					int dY = newY - startY;
					Log.i(TAG, "偏移量:" + dX + "," + dY);
					// 更新UI
					int l = view.getLeft() + dX;
					int t = view.getTop() + dY;
					int r = view.getRight() + dX;
					int b = view.getBottom() + dY;
					view.layout(l, t, r, b);
					view.invalidate();
					// 重新初始化起点坐标的位置
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					break;
				default:
					break;
				}
				// 事件处理完成,停止父控件的响应事件
				return false;
			}
		});
	}

4、手势识别器,实现Activity之间的切换

/**   
* @desc 手势识别器,实现多个功能之间的滑动切换
* @date 2014年11月11日 下午3:02:53 
* @user X1ong 
*/ 
public abstract class BaseGuideActivity extends Activity {
	/** 定义手势识别器 */
	private GestureDetector detector;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		detector = new GestureDetector(this, new SimpleOnGestureListener() {
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				// 屏蔽斜滑动
				if (Math.abs(e2.getRawY() - e1.getRawY()) > 100) {
					return true;
				}
				// 屏蔽滑动慢
				if (Math.abs(velocityX) < 200) {
					return true;
				}
 				if ((e2.getRawX() - e1.getRawX()) > 200) {
					// 上一个页面 从左至右
					showBack();
					return true;
				}
				if ((e1.getRawX() - e2.getRawX()) > 200) {
					// 下一个页面 从右至左
					showNext();
					return true;
				}
				return super.onFling(e1, e2, velocityX, velocityY);
			}

		});
	}

	public abstract void showNext();

	public abstract void showBack();

	/** 使用手势识别器 */
	
	public boolean onTouchEvent(MotionEvent event) {
		detector.onTouchEvent(event);
		return super.onTouchEvent(event);

	}
}

5、跑马灯的文字

<span style="white-space:pre">	</span>/**	    实现原理,让TextView使用都获取焦点,欺骗系统,来实现滚动的效果
<span style="white-space:pre">	</span> * @author X1ong 实现跑马灯的文字
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public class RollTextView extends TextView {

	/** 此处必须实现这个三个构造函数,不然会出错的 */
	public RollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public RollTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RollTextView(Context context) {
		super(context);
	}

	/** 无论有没有焦点,都返回有焦点状态 */
	public boolean isFocused() {
		return true;
	}


6、抖动的文本框和震动

<span style="white-space:pre">		</span>LinearLayout query_layout=(LinearLayout) findViewById(R.id.query_layout);
		Animation animation=AnimationUtils.loadAnimation(this, R.anim.shake);
		query_layout.startAnimation(animation);
		//震动时间,休息时间
		long[] patterm={200,200,300,300};
		//-1不重复  0  循环
		Vibrator vibrator=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);	
		vibrator.vibrate(patterm,-1 );
shake.xml

<span style="white-space:pre">		</span><?xml version="1.0" encoding="utf-8"?>
<span style="white-space:pre">		</span><translate xmlns:android="http://schemas.android.com/apk/res/android"
   <span style="white-space:pre">		</span>  android:duration="1000"
   <span style="white-space:pre">		</span>  android:fromXDelta="0"
    <span style="white-space:pre">		</span>  android:toXDelta="10"
    <span style="white-space:pre">		</span>  android:interpolator="@anim/cycle_7" >
<span style="white-space:pre">		</span></translate>
<span style="white-space:pre">	</span>cycle_7.xml    实现7次循环

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
   android:cycles="7"/> 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值