在抖音无意中刷到了个找女朋友的搞笑视频,但是是web端写的,闲来无事写了移动端的
MainAcitivity.class
package com.tayh.buttontest;
import android.app.ActionBar;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button button;
LinearLayout linear;
Button rightbtn;
private int screenWidth, screenHeight;//屏幕的宽度,高度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
getSupportActionBar().hide();
initView();
}
private void initView(){
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
screenHeight = outMetrics.heightPixels;
button = findViewById(R.id.test_btn);
rightbtn = findViewById(R.id.right_btn);
linear = findViewById(R.id.linear);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPosition();
}
});
rightbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"这就对了!",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 2000);
}
});
}
private void setPosition(){
int viewWidth = button.getWidth();
int viewHeight = button.getHeight();
int x = getRandomInt(screenWidth - viewWidth);
int y =linear.getHeight()+ getRandomInt(screenHeight -linear.getHeight() - viewHeight);
button.setX(x);
button.setY(y);
}
private int getRandomInt(int max) {
Random random = new Random();
return random.nextInt(max);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK||keyCode ==KeyEvent.KEYCODE_HOME){
Toast.makeText(this,"不点好,出不去哦~",Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
layout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:layout_gravity="center_horizontal"
android:text="观察你好久了,做我女朋友好不好?"/>
<Button
android:id="@+id/right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:text="好"
/>
</LinearLayout>
<Button
android:id="@+id/test_btn"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不好"
/>
</LinearLayout>
CD的部分说完了,说点技术上的
1.实现控件位置变化
linear.getHeight是获取不对按钮上方layout距离,这样可以保证随机点都在layout下面。setX,Y是控件位置变化
private void setPosition(){
int viewWidth = button.getWidth();
int viewHeight = button.getHeight();
int x = getRandomInt(screenWidth - viewWidth);
int y =linear.getHeight()+ getRandomInt(screenHeight -linear.getHeight() - viewHeight);
button.setX(x);
button.setY(y);
}
2.这个demo有个bug就是点击Home键也会回到桌面,Back键监听比较简单onKeyDown可以解决,但是Home键不行。网上有很多方法,我也最后没有解决方案,也许是framework层才能搞定,需要大神点播。
但是实现了可以通过系统动态广播监听到home按键,但是不能控制不返回桌面。
HomeWatcherReceiver.class
package com.tayh.buttontest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class HomeWatcherReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "HomeReceiver";
private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(LOG_TAG, "onReceive: action: " + action);
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
// android.intent.action.CLOSE_SYSTEM_DIALOGS
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
Log.i(LOG_TAG, "reason: " + reason);
if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
// 短按Home键
Log.i(LOG_TAG, "homekey");
}
else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
// 长按Home键 或者 activity切换键
Log.i(LOG_TAG, "long press home key or activity switch");
}
else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
// 锁屏
Log.i(LOG_TAG, "lock");
}
else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
// samsung 长按Home键
Log.i(LOG_TAG, "assist");
}
}
}
}
在mainAcitity里注册广播
private static void registerHomeKeyReceiver(Context context) {
Log.i("test", "registerHomeKeyReceiver");
mHomeKeyReceiver = new HomeWatcherReceiver();
final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.registerReceiver(mHomeKeyReceiver, homeFilter);
}
private static void unregisterHomeKeyReceiver(Context context) {
Log.i("test", "unregisterHomeKeyReceiver");
if (null != mHomeKeyReceiver) {
context.unregisterReceiver(mHomeKeyReceiver);
}
}
@Override
protected void onResume() {
super.onResume();
registerHomeKeyReceiver(this);
}
@Override
protected void onStop() {
super.onStop();
unregisterHomeKeyReceiver(this);
}