联系我们 - 广告服务 - 联系电话:
您的当前位置: > 综合 > > 正文

全球热讯:Android代码片段有哪些?Android代码片段详情介绍

来源:CSDN 时间:2023-01-04 11:19:20


(资料图片仅供参考)

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

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;// 电话优先级的UIparams.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文件
android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:gravity="center"    android:orientation="horizontal" >    android:id="@+id/address_show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="2dp"        android:drawableLeft="@android:drawable/ic_menu_call"        android:drawableStart="@android:drawable/ic_menu_call"        android:gravity="center"        android:textSize="20sp" />

2、可移动的Toast窗体

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;// 更新UIwm.updateViewLayout(view, params);// 重新初始化起点坐标的位置startX = (int) event.getRawX();startY = (int) event.getRawY();break;case MotionEvent.ACTION_UP:break;default:break;}// 事件处理完成,停止父控件的响应事件return false;}});

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

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);// 更新UIint 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、跑马灯的文字

/**    实现原理,让TextView使用都获取焦点,欺骗系统,来实现滚动的效果 * @author X1ong 实现跑马灯的文字 */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、抖动的文本框和震动

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
  android:duration="1000"     android:fromXDelta="0"      android:toXDelta="10"      android:interpolator="@anim/cycle_7" >
cycle_7.xml    实现7次循环
 
责任编辑:

标签:

相关推荐:

精彩放送:

新闻聚焦
Top