Afinal的详细使用

activity.main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--FinalActivity-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FinalActivity注解"
        android:id="@+id/btn_afinal_Activity"/>
    <TextView
        android:id="@+id/tv_afinal_Activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
    <!--FinalHttp-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FinalHttp"
        android:id="@+id/btn_afinal_http"/>
    <TextView
        android:id="@+id/tv_afinal_http"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
    <!--FinalDB-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FinalDB"
        android:id="@+id/btn_afinal_DB"/>
    <!--FinalHttp_UpToNet-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Final_UpToNet"
        android:id="@+id/btn_afinal_UpToNet"/>
    <TextView
        android:id="@+id/tv_afinal_UpToNet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
    <!--FinalHttp_DownLoad-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FinalDownload"
        android:id="@+id/btn_afinal_DownLoad"/>
    <TextView
        android:id="@+id/tv_afinal_Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
    <!--FinalBitmap-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FinalBitmap"
        android:id="@+id/btn_afinal_Bitmap"/>
    <ImageView
        android:id="@+id/iv_bitmap"
        android:layout_width="match_parent"
        android:layout_height="150dp" />
</LinearLayout>
Activity中的代码:

package company.com.afinal;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

//1、FinalActivity的用法
public class MainActivity extends FinalActivity {
    @ViewInject(id=R.id.tv_afinal_Activity) TextView tv_activity;
    @ViewInject(id=R.id.tv_afinal_http) TextView tv_http;
    @ViewInject(id=R.id.tv_afinal_Download) TextView tv_Download;
    @ViewInject(id=R.id.tv_afinal_UpToNet) TextView tv_uptonet;
    @ViewInject(id=R.id.iv_bitmap)ImageView imageView;

    @ViewInject(id= R.id.btn_afinal_Activity,click = "btnClick") Button btn_activity;
    @ViewInject(id=R.id.btn_afinal_http,click = "btn_http") Button btn_http;
    @ViewInject(id=R.id.btn_afinal_DB,click = "btn_DB") Button btn_DB;
    @ViewInject(id=R.id.btn_afinal_DownLoad,click = "btn_Download") Button btn_Download;
    @ViewInject(id=R.id.btn_afinal_Bitmap,click = "btn_Bitmap") Button btn_Bitmap;
    @ViewInject(id=R.id.btn_afinal_UpToNet,click = "btn_UpToNet") Button btn_UptoNet;

    public  FinalBitmap fb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        fb=FinalBitmap.create(this);
        fb.configLoadingImage(R.drawable.ic_warn);
        fb.configBitmapMaxHeight(150);
        fb.configBitmapMaxWidth(400);
    }


    public void btnClick(View view){
        tv_activity.setText("你好");
    }
    //2、FinalHttp的使用方法
    public void btn_http(View view){
        FinalHttp fh=new FinalHttp();
        fh.get("www.baidu.com", new AjaxCallBack<Object>() {
            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
                tv_http.setText(current + "/" + count);

            }

            @Override
            public void onSuccess(Object o) {

                tv_http.setText("success");
            }

            @Override
            public void onFailure(Throwable t, String strMsg) {
                super.onFailure(t, strMsg);
                tv_http.setText(strMsg);
            }
        });
    }
    //3、FinalDB的使用
    public void  btn_DB(View view){
        FinalDb db=FinalDb.create(this);
        User user=new User();
        user.setId(1);
        user.setEmail("[email protected]");
        user.setName("fangzhingbing");
        db.save(user);
    }
    //Post测试
    public void btn_UpToNet(View view){
        AjaxParams params=new AjaxParams();
        FinalHttp fh=new FinalHttp();
        try {
            params.put("username", "fangzhingbing");
            params.put("password", "123456");
            params.put("email", "[email protected]");
           // params.put("profile_picture", new File("/mnt/sdcard/pic.jpg")); // 上传文件
           // params.put("profile_picture2", inputStream); // 上传数据流
            //params.put("profile_picture3", new ByteArrayInputStream(bytes)); // 提交字节流
          fh.post("www.baidu.com", params, new AjaxCallBack<Object>() {
              @Override
              public void onSuccess(Object o) {
                  super.onSuccess(o);
                  tv_uptonet.setText("success");
              }

              @Override
              public void onLoading(long count, long current) {
                  super.onLoading(count, current);
                  tv_uptonet.setText(current+"/"+count);
              }

              @Override
              public void onFailure(Throwable t, String strMsg) {
                  super.onFailure(t, strMsg);
                  tv_uptonet.setText(strMsg);
              }
          });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void btn_Download(View view){
        FinalHttp fh=new FinalHttp();
        fh.download("http://www.tompda.com/soft/72849/#div_downloads", "/mnt/sdcard/testapk.apk", new AjaxCallBack() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                tv_Download.setText("success");
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
                tv_Download.setText(count + "/" + current);

            }

            @Override
            public void onFailure(Throwable t, String strMsg) {
                super.onFailure(t, strMsg);
                tv_Download.setText(strMsg);
            }
        });
    }
    //FinalBitmap的使用
    public void btn_Bitmap(View view){
        //加载网络图片
    fb.display(imageView,"http://f.hiphotos.baidu.com/zhidao/pic/item/d53f8794a4c27d1e17ee023c1ad5ad6edcc43879.jpg");
    }
}
点击此处下载Afinal,jar。


你可能感兴趣的:(Afinal的详细使用)