Android简单框架会用到的基类(2)

  BaseNetworkActivity类源码如下:   

import java.util.ArrayList;import java.util.List;
import android.os.Bundle;import android.os.Handler;
import com.lbt.hairdesigner.net.AsyncBaseRequest;
import com.lbt.hairdesigner.net.DefaultThreadPool;

/**
 * 功能描述:应用中需要访问网络的Activity基类
 * @author android_ls
 */
public abstract class BaseNetworkActivity extends BaseActivity {

    /**
     * 当前activity所持有的所有网络请求
     */
    protected List<AsyncBaseRequest> mAsyncRequests;   
    
     /**
     * 当前activity所持有的Handler
     */
    protected Handler mHandler;   

     /**
     * 当前activity所持有的线程池对象
     */
    protected DefaultThreadPool mDefaultThreadPool;   

     @Override
    protected void onCreate(Bundle savedInstanceState) {       
         super.onCreate(savedInstanceState);
    }   

     /**
     * 向用户展示信息前的准备工作在这个方法里处理
     */
    protected void preliminary() {
        mHandler = new Handler();
        mAsyncRequests = new ArrayList<AsyncBaseRequest>();
        mDefaultThreadPool = DefaultThreadPool.getInstance();        
        // 初始化组件
        setupViews();        
        // 初始化数据
        initialized();
    }    

    public List<AsyncBaseRequest> getAsyncRequests() {        
        return mAsyncRequests;
    }   

     public Handler getHandler() {        
        return mHandler;
    }   

     public DefaultThreadPool getDefaultThreadPool() {      
          return mDefaultThreadPool;
    }    

    @Override
    protected void onPause() {        
        /**
         * 在activity暂停的时候,同时设置终止标识,终止异步线程
         */
        cancelAllRequest();        
        super.onPause();
    }    

    @Override
    protected void onDestroy() {        
        /**
         * 在activity销毁的时候,同时设置终止标识,终止异步线程
         */
        cancelAllRequest();        
        super.onDestroy();
    }   

     /**
     * 取消当前Activity相关的网络请求
     */
    private void cancelAllRequest() {        
        if (mAsyncRequests != null && mAsyncRequests.size() > 0) {            
            try {                
                int size = mAsyncRequests.size();               
                for (int i = 0; i < size; i++) {
                    AsyncBaseRequest request = mAsyncRequests.get(i);
                    request.setInterrupted(true);

                    mAsyncRequests.remove(request);
                    size = mAsyncRequests.size();
                }
            } catch (Exception e) {               
                e.printStackTrace();
            }
        }
    }
    
}

  PhotoNetworkActivity类源码如下:

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import com.lbt.hairdesigner.utils.Constant;

/**
 * 功能描述:内置网络请求接口和照片处理的Activity基类
 * @author android_ls
 */
public abstract class PhotoNetworkActivity extends BaseNetworkActivity {

    /**
     * 照片存储路径
     * @return
     */
    protected abstract String getPhotoPath();    

    /**
     * 照片处理完成后的结果
     * @param bitmap
     */
    protected abstract void onPhotoResult(Bitmap bitmap);    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        
        if (Constant.REQUEST_CAMERA_CODE == requestCode) {            
            if (resultCode == Activity.RESULT_OK) {
                System.err.println("getPhotoPath() = " + getPhotoPath());
                File file = new File(getPhotoPath());                
                if (file.exists()) {
                    showToast("拍摄的照片已存好");
                    cropPhoto(Uri.fromFile(file));
                }
            } else {
                showToast("取消了拍照");
            }
        } else if (Constant.REQUEST_GALLERY_CODE == requestCode) {            
            if (resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                showToast("照片已选取");
                cropPhoto(uri);
            } else {
                showToast("取消了选取照片");
            }
        } else if (Constant.REQUEST_CROP_PHOTO_CODE == requestCode) {            
            if (resultCode == Activity.RESULT_OK) {
                photoCropResult(data);
            } else {
                showToast("取消了剪裁照片");
            }
        }

    }    

    /**
     * 调用系统裁剪照片应用
     * @param uri Uri
     */
    private void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("scale", true);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, Constant.REQUEST_CROP_PHOTO_CODE);
    }    

    /**
     * 剪裁完成后返回的Bitmap
     * @param data Intent
     */
    private void photoCropResult(Intent intent) {
        Bundle extras = intent.getExtras();        
        if (extras != null) {
            Bitmap bitmap = extras.getParcelable("data");            
            if (bitmap != null) {
                showToast("裁剪照片成功");
                onPhotoResult(bitmap);
            }
        } else {
            showToast("获取裁剪照片出错");
        }
    }

}

    BaseFrameLayout类源码如下:

import java.util.List;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.lbt.hairdesigner.net.AsyncBaseRequest;
import com.lbt.hairdesigner.net.DefaultThreadPool;
import com.lbt.hairdesigner.utils.LogUtil;
import com.lbt.hairdesigner.widget.TopNavbar;

/**
 * 功能描述:主界面中各个视图的父类
 * @author android_ls
 */
public abstract class BaseFrameLayout extends FrameLayout {

    /**
     * LOG打印标签
     */
    protected static final String TAG = BaseFrameLayout.class.getSimpleName();    
    protected TopNavbar topMenuNavbar;    
    public TopNavbar getTopMenuNavbar() {        
        return topMenuNavbar;
    }   

     /**
     * 获取顶部左侧的menu组件
     * @return
     */
    public LinearLayout getMenuView() {        
        return topMenuNavbar.llShowMenu;
    }    

    /**
     * 应用主界面
     */
    protected BaseNetworkActivity mActivity;    

    /**
     * 当前activity所持有的所有网络请求
     */
    protected List<AsyncBaseRequest> mAsyncRequests;    

    /**
     * 当前activity所持有的Handler
     */
    protected Handler mHandler;    
    
    /**
     * 当前activity所持有的线程池对象
     */
    protected DefaultThreadPool mDefaultThreadPool;    

    protected LayoutInflater mLayoutInflater;    

    /**
     * 当前视图的父容器
     */
    protected LinearLayout viewRoot;    

    public BaseFrameLayout(BaseNetworkActivity activity) {        
        super(activity);

        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(getContext());        
        this.mAsyncRequests = activity.getAsyncRequests();        
        this.mDefaultThreadPool = activity.getDefaultThreadPool();        
        this.mHandler = activity.getHandler();        
        // 向用户展示信息前的准备工作在这个方法里处理
        preliminary();
    }    

    /**
     * 向用户展示信息前的准备工作在这个方法里处理
     */
    protected void preliminary() {        
        // 初始化组件
        setupViews();       
        // 初始化数据
        initialized();
    }    

    /**
     * 初始化组件
     */
    protected void setupViews() {        
        int layoutId = getLayoutId();        
        if (layoutId != 0) {
            viewRoot = (LinearLayout) mLayoutInflater.inflate(layoutId, null);
            addView(viewRoot);

            topMenuNavbar = (TopNavbar) viewRoot.findViewById(R.id.rl_top_menu_navbar);
            topMenuNavbar.ivRefresh.setVisibility(View.GONE);
            topMenuNavbar.ivRightLine.setVisibility(View.GONE);
            topMenuNavbar.tvRightOperationName.setVisibility(View.GONE);
            topMenuNavbar.ivDownListIcon.setVisibility(View.GONE);
        }

    }    

    /**
     * 布局文件ID
     * @return
     */
    protected abstract int getLayoutId();    

    /**
     * 初始化数据
     */
    protected abstract void initialized();    
 
}

PhotoFrameLayout类源码如下:  

import java.io.File;import java.util.UUID;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import com.lbt.hairdesigner.utils.Constant;

/**
 * 功能描述:内置底部滑出拍照菜单的视图父类
 * @author android_ls
 */
public abstract class PhotoFrameLayout extends BaseFrameLayout {

    /**
     * 底部弹出菜单
     */
    protected LinearLayout llPopupMenu;    

    /**
     * 底部弹出菜单照相机
     */
    private Button btnPopupMenuCamera;    

    /**
     * 底部弹出菜单我的相册
     */
    private Button btnPopupMenuPhoto;    

    /**
     * 底部弹出菜单取消按钮
     */
    private Button btnPopupMenuCancel;    

    /**
     * 存放拍照上传的照片路径
     */
    public String mUploadPhotoPath;    

    public PhotoFrameLayout(BaseNetworkActivity activity) {        
        super(activity);
    }    

    /**
     * 初始化组件
     */
    protected void setupViews() {        
        super.setupViews();       
         // 底部的弹出菜单
        llPopupMenu = (LinearLayout) this.findViewById(R.id.ll_popup_menu);
        btnPopupMenuCamera = (Button) this.findViewById(R.id.btn_popup_menu_camera);
        btnPopupMenuPhoto = (Button) this.findViewById(R.id.btn_popup_menu_photo);
        btnPopupMenuCancel = (Button) this.findViewById(R.id.btn_popup_menu_cancel);

        popupMenu();

    }    

    /**
     * 底部的弹出菜单事件处理
     */
    private void popupMenu() {        
        // 点击照相机按钮事件处理
        btnPopupMenuCamera.setOnClickListener(new View.OnClickListener() {            
            public void onClick(View v) {
                llPopupMenu.setVisibility(View.GONE);

                String rootDir = Constant.getRootDir(mActivity);
                File dir = new File(rootDir + File.separator + Constant.IMAGE_CAMERA_UPLOAD_PATH);                if (!dir.exists()) {
                    dir.mkdirs();
                }

                mUploadPhotoPath = rootDir + "/" + Constant.IMAGE_CAMERA_UPLOAD_PATH + "/" + UUID.randomUUID().toString();
                File file = new File(mUploadPhotoPath);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                mActivity.startActivityForResult(intent, Constant.REQUEST_CAMERA_CODE);
            }
        });        

        // 点击相册按钮事件处理
        btnPopupMenuPhoto.setOnClickListener(new View.OnClickListener() {            
            public void onClick(View v) {
                llPopupMenu.setVisibility(View.GONE);

                Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
                getImage.addCategory(Intent.CATEGORY_OPENABLE);
                getImage.setType("image/*");
                mActivity.startActivityForResult(getImage, Constant.REQUEST_GALLERY_CODE);
            }
        });        

        // 点击取消按钮事件处理
        btnPopupMenuCancel.setOnClickListener(new View.OnClickListener() {            
            public void onClick(View v) {

                TranslateAnimation animation = new TranslateAnimation(0f, 0f, 1f, 400.0f);
                animation.setDuration(200);
                animation.setAnimationListener(new AnimationListener() {                    
                    @Override
                    public void onAnimationStart(Animation animation) { }                    

                    @Override
                    public void onAnimationRepeat(Animation animation) { }                    

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        llPopupMenu.setVisibility(View.GONE);
                    }
                });
                llPopupMenu.startAnimation(animation);
            }
        });
    }
}


你可能感兴趣的:(Android简单框架会用到的基类(2))