实例介绍
【实例简介】本项目是一个基于安卓的开源电子书阅读器项目,基于开源的FBREADER1.8.2项目进行的二次开发,支持大文件(亲测5.6MB TXT文件正常打开,就是加载慢点)支持滚屏、翻书、左右滑动三种阅读方式。支持扫描SD卡文件并且添加到书架或者收藏,支持书签功能、支持TXT,HTML,OEB,MOBI,EPUB等格式文件的阅读。本项目代码量很大,是一个比较完善的工程,源码的阅读分析需要一定的安卓基础和耐心,有少量注释。默认编译版本4.1.2编码UTF-8。
【实例截图】
【核心代码】
package yuku.ambilwarna; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.graphics.Color; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.RelativeLayout; import com.yamin.reader.R; public class AmbilWarnaDialog { public interface OnAmbilWarnaListener { void onCancel(AmbilWarnaDialog dialog); void onOk(AmbilWarnaDialog dialog, int color); } final AlertDialog dialog; final OnAmbilWarnaListener listener; final View viewHue; final AmbilWarnaKotak viewSatVal; final ImageView viewCursor; final View viewOldColor; final View viewNewColor; final ImageView viewTarget; final ViewGroup viewContainer; final float[] currentColorHsv = new float[3]; /** * create an AmbilWarnaDialog. call this only from OnCreateDialog() or from a background thread. * * @param context * current context * @param color * current color * @param listener * an OnAmbilWarnaListener, allowing you to get back error or */ public AmbilWarnaDialog(final Context context, int color, OnAmbilWarnaListener listener) { this.listener = listener; Color.colorToHSV(color, currentColorHsv); final View view = LayoutInflater.from(context).inflate(R.layout.ambilwarna_dialog, null); viewHue = view.findViewById(R.id.ambilwarna_viewHue); viewSatVal = (AmbilWarnaKotak) view.findViewById(R.id.ambilwarna_viewSatBri); viewCursor = (ImageView) view.findViewById(R.id.ambilwarna_cursor); viewOldColor = view.findViewById(R.id.ambilwarna_warnaLama); viewNewColor = view.findViewById(R.id.ambilwarna_warnaBaru); viewTarget = (ImageView) view.findViewById(R.id.ambilwarna_target); viewContainer = (ViewGroup) view.findViewById(R.id.ambilwarna_viewContainer); viewSatVal.setHue(getHue()); viewOldColor.setBackgroundColor(color); viewNewColor.setBackgroundColor(color); viewHue.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) { float y = event.getY(); if (y < 0.f) y = 0.f; if (y > viewHue.getMeasuredHeight()) y = viewHue.getMeasuredHeight() - 0.001f; // to avoid looping from end to start. float hue = 360.f - 360.f / viewHue.getMeasuredHeight() * y; if (hue == 360.f) hue = 0.f; setHue(hue); // update view viewSatVal.setHue(getHue()); moveCursor(); viewNewColor.setBackgroundColor(getColor()); return true; } return false; } }); viewSatVal.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) { float x = event.getX(); // touch event are in dp units. float y = event.getY(); if (x < 0.f) x = 0.f; if (x > viewSatVal.getMeasuredWidth()) x = viewSatVal.getMeasuredWidth(); if (y < 0.f) y = 0.f; if (y > viewSatVal.getMeasuredHeight()) y = viewSatVal.getMeasuredHeight(); setSat(1.f / viewSatVal.getMeasuredWidth() * x); setVal(1.f - (1.f / viewSatVal.getMeasuredHeight() * y)); // update view moveTarget(); viewNewColor.setBackgroundColor(getColor()); return true; } return false; } }); dialog = new AlertDialog.Builder(context) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (AmbilWarnaDialog.this.listener != null) { AmbilWarnaDialog.this.listener.onOk(AmbilWarnaDialog.this, getColor()); } } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (AmbilWarnaDialog.this.listener != null) { AmbilWarnaDialog.this.listener.onCancel(AmbilWarnaDialog.this); } } }) .setOnCancelListener(new OnCancelListener() { // if back button is used, call back our listener. @Override public void onCancel(DialogInterface paramDialogInterface) { if (AmbilWarnaDialog.this.listener != null) { AmbilWarnaDialog.this.listener.onCancel(AmbilWarnaDialog.this); } } }) .create(); // kill all padding from the dialog window dialog.setView(view, 0, 0, 0, 0); // move cursor & target on first draw ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { moveCursor(); moveTarget(); view.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); } protected void moveCursor() { float y = viewHue.getMeasuredHeight() - (getHue() * viewHue.getMeasuredHeight() / 360.f); if (y == viewHue.getMeasuredHeight()) y = 0.f; RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewCursor.getLayoutParams(); layoutParams.leftMargin = (int) (viewHue.getLeft() - Math.floor(viewCursor.getMeasuredWidth() / 2) - viewContainer.getPaddingLeft()); ; layoutParams.topMargin = (int) (viewHue.getTop() y - Math.floor(viewCursor.getMeasuredHeight() / 2) - viewContainer.getPaddingTop()); ; viewCursor.setLayoutParams(layoutParams); } protected void moveTarget() { float x = getSat() * viewSatVal.getMeasuredWidth(); float y = (1.f - getVal()) * viewSatVal.getMeasuredHeight(); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewTarget.getLayoutParams(); layoutParams.leftMargin = (int) (viewSatVal.getLeft() x - Math.floor(viewTarget.getMeasuredWidth() / 2) - viewContainer.getPaddingLeft()); layoutParams.topMargin = (int) (viewSatVal.getTop() y - Math.floor(viewTarget.getMeasuredHeight() / 2) - viewContainer.getPaddingTop()); viewTarget.setLayoutParams(layoutParams); } private int getColor() { return Color.HSVToColor(currentColorHsv); } private float getHue() { return currentColorHsv[0]; } private float getSat() { return currentColorHsv[1]; } private float getVal() { return currentColorHsv[2]; } private void setHue(float hue) { currentColorHsv[0] = hue; } private void setSat(float sat) { currentColorHsv[1] = sat; } private void setVal(float val) { currentColorHsv[2] = val; } public void show() { dialog.show(); } public AlertDialog getDialog() { return dialog; } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)