介绍android Afinal框架功能:
Afinal是一个开源的android的orm和ioc应用开发框架。在android应用开发中,通过Afinal的ioc框架,诸如UI绑定,事件绑定,通过注解可以自动绑定。
通过Afinal的orm框架,无需任何配置信息,一行代码就可以对android的sqlit数据库进行增删改查操作。同时Afinal内嵌了finalHttp等简单易用的工具可以轻松对http操作。
下载地址 https://github.com/yangfuhai/afinal
Afinal只是框架,继承了注释、数据库操作、网络请求、图片加载功能。
下面一一举例。
一:创建android项目 (略)
二:导入Afinaljar包
三:使用Afinal
3.1 权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3.2 注释功能
- 完全注解方式就可以进行UI绑定和事件绑定。
- 无需findViewById和setClickListener等。
3.2.1布局
.......
<Button
android:id="@+id/btn_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_db" /> <Button
android:id="@+id/btn_bitmap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_bitmap" /> <Button
android:id="@+id/btn_gethtml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_html"/> <Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_download"/> <Button
android:id="@+id/btn_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_upload"/> <ImageView
android:id="@+id/iv_bitmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/tv_html"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
.......
3.1.2 实现注释功能
1.继承 FinalActivity
public class MainActivity extends FinalActivity
2.@ViewInject()方法加载id
...
@ViewInject(id = R.id.iv_bitmap)
ImageView iv_bitmap; @ViewInject(id = R.id.tv_html)
TextView tv_html; @ViewInject(id = R.id.btn_db, click = "start_db")
Button btn_db; public void start_db(View v) {
tv_html.setText("start_db()...");
} @ViewInject(id = R.id.btn_bitmap, click = "start_bitmap")
Button btn_bitmap; public void start_bitmap(View v) {
tv_html.setText("start_bitmap()...");
} @ViewInject(id = R.id.btn_gethtml, click = "start_gethtml")
Button btn_gethtml; public void start_gethtml(View v) {
tv_html.setText("start_gethtml()...");
} @ViewInject(id = R.id.btn_download, click = "start_download")
Button btn_download; public void start_download(View v) {
tv_html.setText("start_download()...");
} @ViewInject(id = R.id.btn_upload, click = "start_upload")
Button btn_upload; public void start_upload(View v) {
tv_html.setText("start_upload()...");
}
...
3.2 数据库操作
3.2.1跳转到新的Activity
3.2.2 布局及注释
<Button
android:id="@+id/btn_add_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_add_db"/> <Button
android:id="@+id/btn_del_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_del_db"/> <Button
android:id="@+id/btn_update_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_update_db"/> <Button
android:id="@+id/btn_query_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_query_db"/> <TextView
android:id="@+id/tv_result_db"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
public class FinalDBActivity extends FinalActivity { @ViewInject(id = R.id.tv_result_db)
TextView tv_result_db;
@ViewInject(id = R.id.btn_add_db, click = "add_db")
Button btn_add_db; public void add_db(View v) {
tv_result_db.setText("add_db()...");
} @ViewInject(id = R.id.btn_del_db, click = "del_db")
Button btn_del_db; public void del_db(View v) {
tv_result_db.setText("del_db()...");
} @ViewInject(id = R.id.btn_update_db, click = "update_db")
Button btn_update_db; public void update_db(View v) {
tv_result_db.setText("update_db()...");
} @ViewInject(id = R.id.btn_query_db, click = "query_db")
Button btn_query_db; public void query_db(View v) {
tv_result_db.setText("query_db()...");
}
...
3.2.3 数据库实现过程
1.创建数据库对象
FinalDb db = FinalDb.create(mContext, "mytest.db", true);
2.创建实体bean
@Table(name = "user") 调用这句话会在mytest.db数据库中创建user表
@Table(name = "user")//@Table 表示orm(对象关系映射)的表名
public class User {
private int id;
private String name;
private String email;
private Date registerDate;
private Double money;
//get、set 方法 alt+insert
...
3.新增数据
public void add_db(View v) {
tv_result_db.setText("add_db()...");
User user = new User();
user.setEmail("afinal@tsz.net");
user.setName("Android探索者");
user.setRegisterDate(new Date());
user.setMoney(25.5);
db.save(user);
user.setEmail("javabcys@jbcys.com");
user.setName("java编程艺术");
user.setRegisterDate(new Date());
user.setMoney(35.5);
db.save(user);
user.setEmail("android fkjy@fkjy.com");
user.setName("Android 疯狂讲义");
user.setRegisterDate(new Date());
user.setMoney(38.5);
db.save(user);
}
4、查询数据
public void query_db(View v) {
tv_result_db.setText("query_db()...\r\n");
List<User> userList = db.findAll(User.class,"name like '%m%'");//查询所有的用户 tv_result_db.append("用户数量:"+ (userList!=null?userList.size():0)+"\r\n");
if (userList != null && userList.size()>0) {
for (int i = 0;i<userList.size();i++)
tv_result_db.append(userList.get(i).getName() + ":" + userList.get(i).getRegisterDate() + "\r\n");
}
}
结果
5、修改数据
public void update_db(View v) {
tv_result_db.setText("update_db()...");
// User user = new User();
// user.setMoney(111.0);
// db.update(user,"id = 1");//根据where条件更新
User user = new User();
user.setId(2); /////这个属性必须要有,表示 update user set ....where id =2;
user.setMoney(43.5);
user.setName("Java疯狂讲义");
user.setEmail("javafkjy@fkjy.com");
user.setRegisterDate(new Date());
db.update(ueser);
}
查询结果
6、删除数据
public void del_db(View v) {
tv_result_db.setText("del_db()...");
// db.delete(user); //根据对象主键进行删除
// db.deleteById(user, 1); //根据主键删除数据
// db.deleteByWhere(User.class, "money=2.0"); //自定义where条件删除
db.deleteById(User.class,11);
//db.deleteAll(User.class); //删除Bean对应的数据表中的所有数据
}
查询结果
3.3 图片加载功能
AFinal还提供了文件下载的功能,我们只需要传入url和下载路径即可,当然还有回调,在回调方法里面提供了onStart(),onSuccess(),onLoading(),onFailure()四个方法来供我们使用,十分方便。
3.3.1 实现方法
FinalBitmap finalBitmap = FinalBitmap.create(this);
public void start_bitmap(View v) {
tv_html.setText("start_bitmap()...");
String pic_url = "http://imgq.duitang.com/uploads/item/201403/05/20140305105955_5mhet.jpeg";
FinalBitmap finalBitmap = FinalBitmap.create(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
//普通加载
// finalBitmap.display(iv_pic,pic_url);
//带加载动画
// finalBitmap.display(iv_pic,pic_url,bitmap);
//带加载动画以及加载失败显示的图片
finalBitmap.display(iv_bitmap, pic_url, bitmap, bitmap); }
3.3.2 实现效果
3.4 加载网络文本
FinalHttp fHttp= new FinalHttp();
fHttp.get(url,new AjaxCallBack<String>())...
public void start_gethtml(View v) {
tv_html.setText("start_gethtml()...");
FinalHttp fHttp = new FinalHttp();
String url = "http://www.baidu.com";
fHttp.get(url, new AjaxCallBack<String>() { @Override
public void onStart() {
super.onStart();
tv_html.setText("----------start----------\r\n");
} @Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
tv_html.append("--------"+current+"---------"+count+"\r\n");
} @Override
public void onSuccess(String t) {
super.onSuccess(t);
tv_html.append("-------end---------\r\n");
if (!TextUtils.isEmpty(t))
{
tv_html.append(t);
} } @Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
tv_html.append("-------end---------");
tv_html.append("错误码 :"+errorNo+" 错误信息:"+strMsg);
}
});
}
3.5 下载文件
3.5.1实现方法
public void start_download(View v) {
tv_html.setText("start_download()...");
FinalHttp finalHttp = new FinalHttp();
String url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
String target = getFilesDir()+"/afinalmusic.mp4";
finalHttp.download(url, target, new AjaxCallBack<File>() {
@Override
public void onStart() {
super.onStart();
tv_html.setText("--------- start---------\r\n");
} @Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
tv_html.append("---------" +current+" --- "+count+"---------\r\n");
} @Override
public void onSuccess(File file) {
super.onSuccess(file);
tv_html.append("---------end---------\r\n");
if (file!=null && file.exists())
{
tv_html.append("file path : "+file.getAbsolutePath());
Intent mp4Intent = new Intent(MainActivity.this,PlayMp4Activity.class);
mp4Intent.putExtra("file_path",file.getAbsolutePath());
startActivity(mp4Intent);
}
} @Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
tv_html.append("错误码:"+errorNo+", 错误信息 :"+strMsg);
}
});
}
3.5.2 补充:视频播放代码
public class PlayMp4Activity extends FinalActivity { @ViewInject(id = R.id.btn_start,click = "start_play")
Button btn_play;
@ViewInject(id = R.id.btn_pause,click = "pause_play")
Button btn_pause;
@ViewInject(id = R.id.btn_continue,click = "continue_play")
Button btn_continue;
@ViewInject(id = R.id.btn_end,click = "stop_play")
Button btn_stop;
@ViewInject(id = R.id.videoView)
VideoView videoView;
private MediaController mediaController;
public void start_play(View v)
{
Log.e("panzqww","currentpostion = "+videoView.getCurrentPosition());
if (videoView.getCurrentPosition() == 0)
{
initMedia();
videoView.start();
}else{
if (!videoView.isPlaying())
videoView.start();
}
}
public void pause_play(View v)
{
if (videoView.isPlaying())
videoView.pause();
}
public void continue_play(View v)
{
videoView.start();
}
public void stop_play(View v)
{
videoView.stopPlayback();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_mp4);
mediaController=new MediaController(this);
//用来设置控制台样式
videoView.setMediaController(mediaController);
//用来设置起始播放位置,为0表示从开始播放
videoView.seekTo(0);
//播放完成后回调
videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());
mediaController.setMediaPlayer(videoView);
videoView.requestFocus();
initMedia();
}
private void initMedia()
{
String path = getIntent().getStringExtra("file_path");
File file = new File(path);
if (path != null && file.exists())
{
//用来设置要播放的mp4文件
videoView.setVideoPath(path);
//videoView.setVideoURI(Uri.parse(uri));//播放网络视频 uri传入网络地址
if (mediaController == null)
{
Log.d("panzqww","mediaController is null");
mediaController.setMediaPlayer(videoView);
}
}
}
class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener { @Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText( PlayMp4Activity.this, "播放完了", Toast.LENGTH_SHORT).show();
}
}
}
3.6 上传文件
3.6.1搭建服务器 FileUpload.war
https://pan.baidu.com/s/1K5NzUP0F_hEHSiWMZkgIzA
1、将.war文件复制到tomcat服务器webapps下,启动服务器即可 启动后会创建FileUpload目录
2、访问工程路径http://localhost:8080/FileUpload/index.jsp即可测试上传
3.6.2 上传到服务器
public void start_upload(View v) {
tv_html.setText("start_upload()...");
FinalHttp finalHttp = new FinalHttp();
String upload_path = "http://192.168.12.80:8080/FileUpload/FileUploadServlet";
AjaxParams params = new AjaxParams();
try {
File file = new File(getFilesDir()+"/afinalmusic.mp4");
if (file!=null && file.exists()) {
params.put("File", new File(getFilesDir() + "/afinalmusic.mp4"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finalHttp.post(upload_path, params, new AjaxCallBack<Object>() {
@Override
public void onStart() {
super.onStart();
tv_html.append("开始上传...\r\n");
} @Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
tv_html.append("正在上传-----"+current+" ----- "+count+"\r\n"); } @Override
public void onSuccess(Object o) {
super.onSuccess(o);
tv_html.append("上传成功----\r\n");
} @Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
tv_html.append("上传失败----错误码:"+errorNo+"/ 错误信息:"+strMsg+"\r\n");
}
});
}
4.0 实例
https://github.com/MichealPan9999/Afinal