アプリ制作テンプレート

自分で使ってるのをメモ

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WindowData.init(getApplicationContext(),getWindowManager());

    Util.callLog("MainActivity:onCreate");

    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Page1Fragment frag1 = new Page1Fragment();
    fragmentTransaction.replace(R.id.frag, frag1);
    fragmentTransaction.commit();

}

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/frag"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></LinearLayout>
</LinearLayout>

Page1Fragment

public class Page1Fragment extends Fragment implements BaseFragment{

    private View rootView;
    private CallbackFunction callback;
    private Boolean finishFlag;
    private Handler mHandler;

    public Page1Fragment() {
        Util.callLog("CONSTRUCTOR:" + this.getClass().getCanonicalName());
        finishFlag = false;
    }

    public void setOnCallbackFunction(CallbackFunction _callback) {
        callback = _callback;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        Util.callLog("CREATE:" + this.getClass().getCanonicalName());
        rootView = inflater.inflate(R.layout.fragment_page1, container, false);
        rootView.setClickable(true);
        mHandler = new Handler();

        AlphaAnimation aa = new AlphaAnimation(0f,1f);
        aa.setDuration(600);
        rootView.startAnimation(aa);
        return rootView;
    }

    @Override
    public void callFinish(BaseCallback callback) {
        Util.callLog("CALLFinish:" + this.getClass().getCanonicalName());
        if (finishFlag) {
            return;
        }
        finishFlag = true;
        callback.callbackFunction();
    }

    @Override
    public void onDestroy() {
        Util.callLog("DESTROY:" + this.getClass().getCanonicalName());

        rootView = null;
        callback = null;

        super.onDestroy();
    }

    public interface CallbackFunction {
        //BASEの方からこのfragmentを取り外してもらう用に用意しておく。
        public void callbackFinish();
    }
}

BaseFragment

public interface BaseFragment {
    /*
    フラグメントを削除する時にアニメーションさせる。
     */
    void callFinish(BaseCallback callback);
    interface BaseCallback{
        void callbackFunction();
    }

}

fragment layout

<FrameLayout 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"
    tools:context="yaya.tokyo.templateandroid.Page1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
    <ImageView
        android:id="@+id/hello"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

WindowData

public class WindowData {
    private static final float baseSize = 750f;
    public static int height;
    public static int width;
    public static float scale;
    public static Matrix matrix;
    public static AssetManager amanager;

    public static void init(Context cntxt,WindowManager wm){
        DisplayMetrics displaymetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displaymetrics);
        height = displaymetrics.heightPixels;//-getStatusBarHeight(cntxt);
        width = displaymetrics.widthPixels;
        scale = (float) displaymetrics.widthPixels / baseSize;
        matrix = new Matrix();
        matrix.postScale(WindowData.scale, WindowData.scale);
        amanager = cntxt.getAssets();
    }

    public static int getStatusBarHeight(Context cntxt) {
        int result = 0;
        int resourceId = cntxt.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = cntxt.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

Util

public class Util {

    public static void callLog(String log){
        if(GlobalData.DEBUG_MODE) {
            Log.e(GlobalData.app_name, log);
        }
    }
    public static void callLog(int log){
        if(GlobalData.DEBUG_MODE) {
            Log.e(GlobalData.app_name, "INTEGER:"+log);
        }
    }
    public static void callLog(float log){
        if(GlobalData.DEBUG_MODE) {
            Log.e(GlobalData.app_name, "FLOAT:"+log);
        }
    }

    public static Bitmap loadBitmapFromAssetRow(Context context,String url) {

        Bitmap bm = null;
        try {
            bm = loadBitmapFromAssetRowFunc(context,url);
        } catch (Exception e) {
        } catch (Error e) {}
        if(bm == null){
            System.gc();
            try {
                bm = loadBitmapFromAssetRowFunc(context,url);
            } catch (Exception e) {
            } catch (Error e) {}
        }
        if(bm == null){
            bm = Util.loadBitmapFromAsset(context, "null.png");
        }
        return bm;
    }

    public static Bitmap loadBitmapFromAsset(Context context,String url) {
        Bitmap bm = null;
        if(bm == null){
            try {
                bm = loadBitmapFromAssetFunc(context,url,true);
            } catch (Exception e) {}
        }
        if(bm == null){
            System.gc();
            try {
                bm = loadBitmapFromAssetFunc(context,url,true);
            } catch (Exception e) {}
        }
        if(bm == null){
            bm = Util.loadBitmapFromAsset(context, "null.png");
        }
        return bm;
    }

    private static BitmapFactory.Options options;

    public static Bitmap loadBitmapFromAssetFunc(Context context,String url,Boolean resize) {

        byte[] w = new byte[1024];
        Bitmap _bitmap;

        BufferedInputStream buf;
        FileInputStream in;
        BufferedInputStream binput;
        ByteArrayOutputStream out;

        if(options != null){
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inPurgeable = true;
        }
        _bitmap = null;
        if(resize){
            try {
                buf = new BufferedInputStream(WindowData.amanager.open(url), 8192);
                _bitmap = BitmapFactory.decodeStream(buf,null,options);
                buf.close();
                buf = null;
            } catch (Exception e) {}
        }
        if(_bitmap == null){
            try {
                in = context.openFileInput(url.replace("/", "__"));
                binput = new BufferedInputStream(in);
                out = new ByteArrayOutputStream();
                while (binput.read(w) >= 0) {
                    out.write(w, 0, 1024);
                }
                byte[] byteData = out.toByteArray();
                _bitmap = BitmapFactory.decodeByteArray(byteData, 0, byteData.length);
                in.close();
                binput.close();
                out.close();

            } catch (Exception e) {

            }finally{

                in = null;
                binput = null;
                out = null;
            }

        }
        if(_bitmap == null){
            return null;
        }

        if(resize){
            Bitmap data = Bitmap.createBitmap(_bitmap, 0, 0,_bitmap.getWidth(), _bitmap.getHeight(), WindowData.matrix, true);
            _bitmap = null;
            return data;
        }else{
            return _bitmap;
        }
    }

    public static Bitmap loadBitmapFromAssetRowFunc(Context context,String url) {
        if(options != null){
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inPurgeable = true;
        }
        Bitmap __bitmap = null;
        byte[] __w = new byte[1024];
        try {
            BufferedInputStream __buf = new BufferedInputStream(WindowData.amanager.open(url), 8192);
            __bitmap = BitmapFactory.decodeStream(__buf, null, options);
            __buf.close();
            __buf = null;
        } catch (Exception e) {
        }

        if(__bitmap == null){
            FileInputStream __in;
            BufferedInputStream __binput;
            ByteArrayOutputStream __out;
            try {
                __in = context.openFileInput(url.replace("/", "__"));
                __binput = new BufferedInputStream(__in);
                __out = new ByteArrayOutputStream();

                while (__binput.read(__w) >= 0) {
                    __out.write(__w, 0, 1024);
                }
                byte[] byteData = __out.toByteArray();
                __bitmap = BitmapFactory.decodeByteArray(byteData, 0, byteData.length);
                __in.close();
                __binput.close();
                __out.close();

            } catch (Exception e) {
            }finally{
                __in = null;
                __binput = null;
                __out = null;
            }

        }

        if(__bitmap == null){
            return null;
        }

        return __bitmap;
    }

}

GlobalData

public class GlobalData {

    //STATIC
    public static final Boolean DEBUG_MODE = false;
    public static final String app_name = "TEMPLATE";

}

SimpleAnimationListener

public class SimpleAnimationListener implements Animation.AnimationListener{
    @Override
    public void onAnimationStart(Animation animation) {

    }
    @Override
    public void onAnimationEnd(Animation animation) {

    }
    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}


割とガチで使ってるテンプレート

前後の記事

前の記事:

次の記事:

関連の記事

コメントの投稿

  • サイト内検索

新作アプリの紹介

関連サイトの紹介

アンドロイドアプリ開発TIPS
きぐるみカメラ
ふらいぱん
アンドロイドのデザイン集
Page top↑