Skip to content

Commit d871050

Browse files
authored
第11章 事件
1 parent 0f87c73 commit d871050

13 files changed

+1288
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.example.event.util;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.nio.ByteBuffer;
9+
import java.util.Locale;
10+
11+
import android.content.Context;
12+
import android.graphics.Bitmap;
13+
import android.graphics.BitmapFactory;
14+
import android.graphics.Matrix;
15+
import android.os.Environment;
16+
import android.util.Log;
17+
18+
public class BitmapUtil {
19+
20+
public static void saveBitmap(String path, Bitmap bitmap, String format, int quality) {
21+
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
22+
if (format.toUpperCase(Locale.getDefault()).equals("PNG") == true) {
23+
compressFormat = Bitmap.CompressFormat.PNG;
24+
}
25+
try {
26+
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
27+
bitmap.compress(compressFormat, quality, bos);
28+
bos.flush();
29+
bos.close();
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
public static void saveBitmap(String path, ByteBuffer buffer,
36+
int sample_size, String format, int quality) {
37+
// File file = new File(path);
38+
// FileOutputStream outputStream = null;
39+
// outputStream = new FileOutputStream(file);
40+
try {
41+
byte[] buff = new byte[buffer.remaining()];
42+
buffer.get(buff);
43+
BitmapFactory.Options ontain = new BitmapFactory.Options();
44+
ontain.inSampleSize = sample_size;
45+
Bitmap bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length, ontain);
46+
saveBitmap(path, bitmap, format, quality);
47+
// outputStream.write(buff);
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
public static Bitmap openBitmap(String path) {
54+
Bitmap bitmap = null;
55+
try {
56+
BufferedInputStream bis = new BufferedInputStream( new FileInputStream(path));
57+
bitmap = BitmapFactory.decodeStream(bis);
58+
bis.close();
59+
} catch (Exception e) {
60+
e.printStackTrace();
61+
}
62+
return bitmap;
63+
}
64+
65+
public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree) {
66+
Matrix matrix = new Matrix();
67+
matrix.postRotate((float) rotateDegree);
68+
Bitmap rotaBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
69+
b.getHeight(), matrix, false);
70+
return rotaBitmap;
71+
}
72+
73+
public static String getCachePath(Context context) {
74+
String path = Environment.getExternalStoragePublicDirectory(
75+
Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/";
76+
return path;
77+
}
78+
79+
public static Bitmap zoomImage(Bitmap origImage, double newWidth, double newHeight) {
80+
// 获取这个图片的宽和高
81+
float width = origImage.getWidth();
82+
float height = origImage.getHeight();
83+
// 创建操作图片用的matrix对象
84+
Matrix matrix = new Matrix();
85+
// 计算宽高缩放率
86+
float scaleWidth = ((float) newWidth) / width;
87+
float scaleHeight = ((float) newHeight) / height;
88+
// 缩放图片动作
89+
matrix.postScale(scaleWidth, scaleHeight);
90+
Bitmap newImage = Bitmap.createBitmap(origImage, 0, 0, (int) width, (int) height, matrix, true);
91+
return newImage;
92+
}
93+
94+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.event.util;
2+
3+
import android.annotation.SuppressLint;
4+
5+
import java.text.DateFormat;
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
8+
9+
/**
10+
* Created by ouyangshen on 2016/9/24.
11+
*/
12+
public class DateUtil {
13+
@SuppressLint("SimpleDateFormat")
14+
public static String getNowDateTime() {
15+
SimpleDateFormat s_format = new SimpleDateFormat("yyyyMMddhhmmss");
16+
return s_format.format(new Date());
17+
}
18+
19+
@SuppressLint("SimpleDateFormat")
20+
public static String getNowTime() {
21+
SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss");
22+
return s_format.format(new Date());
23+
}
24+
25+
@SuppressLint("SimpleDateFormat")
26+
public static String getNowTimeDetail() {
27+
SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss.SSS");
28+
return s_format.format(new Date());
29+
}
30+
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.event.util;
2+
3+
import android.content.Context;
4+
import android.util.DisplayMetrics;
5+
import android.view.WindowManager;
6+
7+
/**
8+
* Created by ouyangshen on 2016/9/11.
9+
*/
10+
public class DisplayUtil {
11+
12+
public static int getSreenWidth(Context ctx) {
13+
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
14+
DisplayMetrics dm = new DisplayMetrics();
15+
wm.getDefaultDisplay().getMetrics(dm);
16+
return dm.widthPixels;
17+
}
18+
19+
public static int getSreenHeight(Context ctx) {
20+
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
21+
DisplayMetrics dm = new DisplayMetrics();
22+
wm.getDefaultDisplay().getMetrics(dm);
23+
return dm.heightPixels;
24+
}
25+
26+
public static float getSreenDensity(Context ctx) {
27+
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
28+
DisplayMetrics dm = new DisplayMetrics();
29+
wm.getDefaultDisplay().getMetrics(dm);
30+
return dm.density;
31+
}
32+
33+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.event.util;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Method;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
import android.annotation.SuppressLint;
9+
import android.content.Context;
10+
import android.util.Log;
11+
import android.view.Menu;
12+
import android.view.ViewConfiguration;
13+
import android.view.Window;
14+
15+
/**
16+
* Created by ouyangshen on 2016/9/11.
17+
*/
18+
public class Utils {
19+
//根据手机的分辨率从 dp 的单位 转成为 px(像素)
20+
public static int dip2px(Context context, float dpValue) {
21+
final float scale = context.getResources().getDisplayMetrics().density;
22+
return (int) (dpValue * scale + 0.5f);
23+
}
24+
25+
//根据手机的分辨率从 px(像素) 的单位 转成为 dp
26+
public static int px2dip(Context context, float pxValue) {
27+
final float scale = context.getResources().getDisplayMetrics().density;
28+
return (int) (pxValue / scale + 0.5f);
29+
}
30+
31+
@SuppressLint("SimpleDateFormat")
32+
public static String getNowDateTime(String format) {
33+
SimpleDateFormat sdf = new SimpleDateFormat(format);
34+
return sdf.format(new Date());
35+
}
36+
37+
//如果设备有物理菜单按键,需要将其屏蔽才能显示OverflowMenu
38+
//API18以下需要该函数在右上角强制显示选项菜单
39+
public static void forceShowOverflowMenu(Context context) {
40+
try {
41+
ViewConfiguration config = ViewConfiguration.get(context);
42+
Field menuKeyField = ViewConfiguration.class.
43+
getDeclaredField("sHasPermanentMenuKey");
44+
if (menuKeyField != null) {
45+
menuKeyField.setAccessible(true);
46+
menuKeyField.setBoolean(config, false);
47+
}
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
// 显示OverflowMenu的Icon
54+
public static void setOverflowIconVisible(int featureId, Menu menu) {
55+
// ActionBar的featureId是8,Toolbar的featureId是108
56+
if (featureId % 100 == Window.FEATURE_ACTION_BAR && menu != null) {
57+
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
58+
try {
59+
Method m = menu.getClass().getDeclaredMethod(
60+
"setOptionalIconsVisible", Boolean.TYPE);
61+
m.setAccessible(true);
62+
m.invoke(menu, true);
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
}
66+
}
67+
}
68+
}
69+
70+
}
71+
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.example.event.widget;
2+
3+
import java.util.ArrayList;
4+
5+
import com.example.event.R;
6+
import com.example.event.util.Utils;
7+
8+
import android.app.Activity;
9+
import android.content.Context;
10+
import android.os.Handler;
11+
import android.util.AttributeSet;
12+
import android.view.GestureDetector;
13+
import android.view.Gravity;
14+
import android.view.LayoutInflater;
15+
import android.view.MotionEvent;
16+
import android.view.View;
17+
import android.widget.ImageView;
18+
import android.widget.RadioButton;
19+
import android.widget.RadioGroup;
20+
import android.widget.RelativeLayout;
21+
import android.widget.ViewFlipper;
22+
23+
public class BannerFlipper extends RelativeLayout {
24+
private static final String TAG = "BannerFlipper";
25+
private Context mContext;
26+
private ViewFlipper mFlipper;
27+
private RadioGroup mGroup;
28+
private LayoutInflater mInflater;
29+
private int dip_15;
30+
private GestureDetector mGesture;
31+
private float mFlipGap = 20f;
32+
33+
public BannerFlipper(Context context) {
34+
this(context, null);
35+
}
36+
37+
public BannerFlipper(Context context, AttributeSet attrs) {
38+
super(context, attrs);
39+
mContext = context;
40+
init();
41+
}
42+
43+
public void start() {
44+
startFlip();
45+
}
46+
47+
public void setImage(ArrayList<Integer> imageList) {
48+
for (int i = 0; i < imageList.size(); i++) {
49+
Integer imageID = ((Integer) imageList.get(i)).intValue();
50+
ImageView iv_item = new ImageView(mContext);
51+
iv_item.setLayoutParams(new LayoutParams(
52+
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
53+
iv_item.setScaleType(ImageView.ScaleType.FIT_XY);
54+
iv_item.setImageResource(imageID);
55+
mFlipper.addView(iv_item);
56+
}
57+
for (int i = 0; i < imageList.size(); i++) {
58+
RadioButton radio = new RadioButton(mContext);
59+
radio.setLayoutParams(new RadioGroup.LayoutParams(dip_15, dip_15));
60+
radio.setGravity(Gravity.CENTER);
61+
radio.setButtonDrawable(R.drawable.indicator_selector);
62+
mGroup.addView(radio);
63+
}
64+
mFlipper.setDisplayedChild(imageList.size() - 1);
65+
startFlip();
66+
}
67+
68+
private void init() {
69+
mInflater = ((Activity) mContext).getLayoutInflater();
70+
View view = mInflater.inflate(R.layout.banner_flipper, null);
71+
mFlipper = (ViewFlipper) view.findViewById(R.id.banner_flipper);
72+
mGroup = (RadioGroup) view.findViewById(R.id.rg_indicator);
73+
addView(view);
74+
dip_15 = Utils.dip2px(mContext, 15);
75+
// 该手势的onSingleTapUp事件是点击时进入广告页
76+
mGesture = new GestureDetector(mContext, new BannerGestureListener());
77+
mHandler.postDelayed(mRefresh, 200);
78+
}
79+
80+
public boolean dispatchTouchEvent(MotionEvent event) {
81+
mGesture.onTouchEvent(event);
82+
return true;
83+
}
84+
85+
final class BannerGestureListener implements GestureDetector.OnGestureListener {
86+
87+
@Override
88+
public final boolean onDown(MotionEvent event) {
89+
return true;
90+
}
91+
92+
@Override
93+
public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
94+
if (e1.getX() - e2.getX() > mFlipGap) {
95+
startFlip();
96+
return true;
97+
}
98+
if (e1.getX() - e2.getX() < -mFlipGap) {
99+
backFlip();
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
@Override
106+
public final void onLongPress(MotionEvent event) {
107+
}
108+
109+
@Override
110+
public final boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
111+
//return false;
112+
// 如果外层是普通的ScrollView,则此处不允许父容器的拦截动作
113+
// CustomScrollActivity里面通过自定义ScrollView,来区分水平滑动还是垂直滑动
114+
// BannerOptimizeActivity使用系统ScrollView,则此处需要下面代码禁止父容器的拦截
115+
if (Math.abs(distanceY) < Math.abs(distanceX)) {
116+
BannerFlipper.this.getParent().requestDisallowInterceptTouchEvent(true);
117+
return true;
118+
} else {
119+
return false;
120+
}
121+
}
122+
123+
@Override
124+
public final void onShowPress(MotionEvent event) {
125+
}
126+
127+
@Override
128+
public boolean onSingleTapUp(MotionEvent event) {
129+
int position = mFlipper.getDisplayedChild();
130+
mListener.onBannerClick(position);
131+
return false;
132+
}
133+
134+
}
135+
136+
private void startFlip() {
137+
mFlipper.startFlipping();
138+
mFlipper.showNext();
139+
}
140+
141+
private void backFlip() {
142+
mFlipper.startFlipping();
143+
mFlipper.showPrevious();
144+
}
145+
146+
private Handler mHandler = new Handler();
147+
private Runnable mRefresh = new Runnable() {
148+
@Override
149+
public void run() {
150+
int pos = mFlipper.getDisplayedChild();
151+
((RadioButton) mGroup.getChildAt(pos)).setChecked(true);
152+
mHandler.postDelayed(this, 200);
153+
}
154+
};
155+
156+
private BannerClickListener mListener;
157+
public void setOnBannerListener(BannerClickListener listener) {
158+
mListener = listener;
159+
}
160+
161+
public static interface BannerClickListener {
162+
public abstract void onBannerClick(int position);
163+
}
164+
165+
}

0 commit comments

Comments
 (0)