使用Java代码的方式实现简易的走势图(1)

本文介绍了如何使用Java代码实现简易走势图,旨在减轻UI压力。通过计算屏幕比例系数适应不同分辨率,并详细解释了核心方法`initScreenParam`和布局定位方法。在FrameLayout中按比例设置控件位置和大小,为实现走势图奠定基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我第二篇优快云文章了,写这篇文章的时候,心态已经发生了些变化。因为最近看了一个朋友介绍的一本书《奇特的一生》。在很多人看来,书中的主人翁柳比歇夫生活、工作的方法饱受争议。但是,在我看来,如果一个普通人,想要在一生之中有点成就,想要给这个世界留点什么有价值的东西,柳比歇夫的方法或许可以起到很大的作用。当我尝试去按照他的方法去做的时候,我的内心变得很纯净,丝毫不受外界各种琐碎之事的影响。另外看过一本书里面的一句话:当你安静的时候,智慧就会升起来。所以,在很多的时候,我就会去静静的思考,思考多了,慢慢地就看开了一些事情,对时间、生命的本质也会有自己的理解。闲话扯远了,今天我想要给大家带来的是关于纯代码实现的简易的的走势图。下面是效果图:

这里写图片描述
1.为了减轻我们UI的压力,我们只出了一套图片是针对分辨率为1280 *720,这样为了解决屏幕的适配的问题,我们必须对不同的手机的屏幕的分辨率对图片进行处理。下面是对控件加载显示的核心的类:

package com.poker175.activity;

import java.io.File;
import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.WeakHashMap;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.poker175.util.StringUtil;
import static android.content.Context.WINDOW_SERVICE;
public class BaseCommand {
    public static String verName;
    public static int verCode;
    public static String pkg;
    public static String imei;
    public static String imsi;
    public static int osver;
    public static String phone;
    public static String sim;
    public static String model;
    public static int sW;
    public static int sH;
    public static String duoapp_channelid;
    public static String sdkVercode;
    public static float radio = 1, radio1 = 1, radio_s = 1;
    public static float radioH;
    public static float radioW;
    public static int deltaX = 0;
    public static int deltaY = 0;
    public static float W = 1280.F;
    public static float H = 720.F;
    static WeakHashMap<String, WeakReference<Bitmap>> bmpCacheMap = new WeakHashMap<String, WeakReference<Bitmap>>();

    /**
     * @param a
     */
    public static void getDeviceInfo(Activity a) {
        if (TextUtils.isEmpty(verName)) {
            PackageManager pm = a.getPackageManager();
            try {
                PackageInfo info = pm.getPackageInfo(a.getPackageName(), 0);
                pkg = info.applicationInfo.packageName;
                verName = info.versionName;
                verCode = info.versionCode;
                TelephonyManager tm = (TelephonyManager) a
                        .getSystemService(Context.TELEPHONY_SERVICE);
                imei = tm.getDeviceId();
                imsi = tm.getSubscriberId();
                phone = tm.getLine1Number();
                sim = tm.getSimSerialNumber();
                osver = VERSION.SDK_INT;
                model = android.os.Build.MODEL;
                // @////@System.out.println("model:" + model);
                DisplayMetrics dm = new DisplayMetrics();
                a.getWindowManager().getDefaultDisplay().getMetrics(dm);

                sW = dm.widthPixels;
                sH = dm.heightPixels;
                float scaleX = (float) sW / 1280;// 以1280*720为标准进行缩放
                float scaleY = (float) sH / 720;
                radio = scaleX >= scaleY ? scaleY : scaleX;
                duoapp_channelid = DuoCommond.duoapp_channelid;
                sdkVercode = DuoCommond.sdkVercode;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @param a
     */
    public static void getDeviceInfo(Service a) {
        if (TextUtils.isEmpty(verName)) {
            PackageManager pm = a.getPackageManager();
            try {
                PackageInfo info = pm.getPackageInfo(a.getPackageName(), 0);
                pkg = info.applicationInfo.packageName;
                verName = info.versionName;
                verCode = info.versionCode;
                TelephonyManager tm = (TelephonyManager) a
                        .getSystemService(Context.TELEPHONY_SERVICE);
                imei = tm.getDeviceId();
                imsi = tm.getSubscriberId();
                phone = tm.getLine1Number();
                sim = tm.getSimSerialNumber();
                osver = VERSION.SDK_INT;
                model = android.os.Build.MODEL;
                WindowManager wm = (WindowManager) a.getSystemService(WINDOW_SERVICE);
                // 具体返回对象是WindowMangerIml类
                Display display = wm.getDefaultDisplay();
                DisplayMetrics dm = new DisplayMetrics();
                display.getMetrics(dm);

                sW = dm.widthPixels;
                sH = dm.heightPixels;

                duoapp_channelid = DuoCommond.duoapp_channelid;
                sdkVercode = DuoCommond.sdkVercode;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @param context
     * @param name
     * @return
     */
    public static String getMeataStringData(Context context, String name) {
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            return bundle.getString(name);
        } catch (Exception e) {

        }
        return "";
    }

    /**
     * @param context
     * @param name
     * @return
     */
    public static int getMeataIntData(Context context, String name) {
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            return bundle.getInt(name, 0);
        } catch (Exception e) {

        }
        return 0;
    }

    /**
     * @param context
     * @param dir
     * @return
     */
    public static String getCachePath(Context context, String dir) {
        String path = null;
        if (TextUtils.isEmpty(dir))
            dir = context.getPackageName();
        String tmp = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(tmp)) {
            // path = "/sdcard/" + dir + "/";
            path = Environment.getExternalStorageDirectory().getPath() + "/"
                    + dir + "/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdir();
            }
            file = null;
            //
        } else {
            path = context.getCacheDir().getAbsolutePath() + File.separator;
        }
        // @////@System.out.println("getCachePath:" + path);
        return path;
    }

    /**
     * @param url
     * @return
     */
    public static String appendUrl(String url) {
        String newurl = url;
        newurl = StringUtil.appendNameValue(newurl, "duoid", duoapp_channelid);
        newurl = StringUtil.appendNameValue(newurl, "sdkVercode", sdkVercode);
        newurl = StringUtil.appendNameValue(newurl, "lang", StringUtil.getLocaleLanguage());
        newurl = StringUtil.appendNameValue(newurl, "width", String.valueOf(sW));
        newurl = StringUtil.appendNameValue(newurl, "height",
                String.valueOf(sH));
        newurl = StringUtil.appendNameValue(newurl, "ver", verName);
        newurl = StringUtil.appendNameValue(newurl, "vercode",
                String.valueOf(verCode));
        newurl = StringUtil.appendNameValue(newurl, "imei", imei);
        newurl = StringUtil.appendNameValue(newurl, "imsi", imsi);
        newurl = StringUtil.appendNameValue(newurl, "osver",
                String.valueOf(osver));
        newurl = StringUtil.appendNameValue(newurl, "pkg", pkg);
        if (!TextUtils.isEmpty(model))
            newurl = StringUtil.appendNameValue(newurl, "model",
                    URLEncoder.encode(model));
        newurl = StringUtil.appendNameValue(newurl, "phone", phone == null ? ""
                : URLEncoder.encode(phone));
        newurl = StringUtil.appendNameValue(newurl, "sim", sim == null ? ""
                : URLEncoder.encode(sim));
        return newurl;
    }

    /**
     * @param input
     * @return
     */
    public static String getMd5Hash(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String md5 = number.toString(16);
            //
            // while (md5.length() < 32)
            // md5 = "0" + md5;
            return md5;
        } catch (NoSuchAlgorithmException e) {
            // Log.e("MD5", e.getMessage());
            return null;
        }
    }

    /**
     * @param handler
     */
    public static void sendProcessingMessage(Handler handler) {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putInt(BaseContant.MESSAGE_DATA_RESULT,
                BaseContant.MESSAGE_PROCESSINGFLAG);
        msg.setData(b);
        handler.sendMessage(msg);
    }

    /**
     * @param handler
     */
    public static void sendProcessedMessage(Handler handler, int pageid,
                                            String url, boolean isClear, boolean isRefresh, boolean isRemote) {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putInt(BaseContant.MESSAGE_DATA_RESULT,
                BaseContant.MESSAGE_PROCESSEDFLAG);
        b.putString(BaseContant.MESSAGE_URL_RESULT, url);
        b.putInt(BaseContant.MESSAGE_PAGEID_RESULT, pageid);
        b.putBoolean(BaseContant.MESSAGE_ISCLEAR_RESULT, isClear);
        b.putBoolean(BaseContant.MESSAGE_ISREFRESH_RESULT, isRefresh);
        b.putBoolean(BaseContant.MESSAGE_ISREMOTE_RESULT, isRemote);
        msg.setData(b);
        handler.sendMessage(msg);
    }

    /**
     * 清理早于指定的时间的问题
     */
    public static void clearSavedFile(String rootPath, int day) {
        File dir = new File(rootPath);
        String[] filenames = dir.list();
        if (filenames == null)
            return;
        // //@////@System.out.println("clearSavedFile 111:");
        long m1 = day * 86400000;// 24 * 60 * 60 * 1000;
        long m2 = System.currentTimeMillis();
        long m = m2 - m1;
        // //@////@System.out.println("filenames.length:" + filenames.length);
        for (String filename : filenames) {
            // //@////@System.out.println("filename:" + filename);
            String[] tmp = filename.split("_");
            if (tmp.length == 2 && TextUtils.isDigitsOnly(tmp[1])) {
                long m3 = Long.parseLong(tmp[1]);
                // Debug.d("m3:" + m3);
                if (m > m3) {
                    // //@////@System.out.println("del:" + filename);
                    File file = new File(rootPath + filename);
                    // file.deleteOnExit();
                    file.delete();
                    file = null;
                }
            } else if (!filename.toLowerCase().endsWith(".apk")
                    && !filename.toLowerCase().endsWith(".ap0")) {
                File file = new File(rootPath + filename);
                // file.deleteOnExit();
                file.delete();
            }
        }
    }

    /**
     * 删除下载在手机中(非SDCARD)的安装包
     */
    public static void clearDownedTempFile(Context context) {
        File file = context.getFilesDir();
        for (String filename : file.list()) {
            if (filename.startsWith("sharetemp_")) {
                // //@////@System.out.println("clearDownedTempFile:" +
                // filename);
                context.deleteFile(filename);
            }
        }
    }

    /**
     * 重置控件宽高
     *
     * @param view
     * @param w
     * @param h
     */
    public static void setViewWH(View view, float w, float h) {
        LayoutParams para = view.getLayoutParams();
        para.width = (int) (w * BaseCommond.radio);// 修改宽度
        para.height = (int) (h * BaseCommond.radio);// 修改高度
        view.setLayoutParams(para); // 设置修改后的布局。
    }

    /**
     * 适配控件相关宽高
     *
     * @param container  父容器
     * @param view       控件
     * @param w          控件宽度
     * @param h          控件高度
     * @param leftMargin 以屏幕左上角为原点 的左边距
     * @param topMargin  以屏幕左上角为原点 的上边距
     * @param isRoot     是否为根容器
     */
    public static void setPositionAndWH(ViewGroup container, View view, int w, int h,
                                        int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            layoutParams.topMargin = (int) (topMargin * radio_s);
        }

        container.addView(view, layoutParams);
    }

    /**
     * 设置大厅的玩家的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setNewHallPlayerInfoAndWH(ViewGroup container, View view, int w, int h,
                                                 int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioH == radioW) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = (int) (topMargin * radio_s / 1.4);
            }

        }
        container.addView(view, layoutParams);
    }

    /**
     * 设置万人场的底部的玩家的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setWanrenBottomPlayerInfo(ViewGroup container, View view, int w, int h,
                                                 int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioH == radioW) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = (int) (topMargin * radioH);
            }

        }
        container.addView(view, layoutParams);
    }

    /**
     * 设置今日活动的RadioButton的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setRadioButtonPositionAndWH(ViewGroup container, View view, int w, int h,
                                                   int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioH == radioW) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = topMargin;
            }

        }
        container.addView(view, layoutParams);
    }

    /**
     * 设置顶部的广播和消息的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setTopBroadcastMessageAndWH(ViewGroup container, View view, int w, int h,
                                                   int leftMargin, int topMargin, Boolean isRoot) {
        RelativeLayout.LayoutParams layoutParams;
        layoutParams = new RelativeLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
        }
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

        container.addView(view, layoutParams);
    }


    /**
     * 设置底部的菜单的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setBottomMenuPositionAndWH(ViewGroup container, View view, int w, int h,
                                                  int leftMargin, int topMargin, Boolean isRoot) {
        RelativeLayout.LayoutParams layoutParams;
        layoutParams = new RelativeLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
        }
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        container.addView(view, layoutParams);
    }

    /**
     * 设置大厅消息和客服消息的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param isRoot
     */
    public static void setHallServiceMessageAndWH(ViewGroup container, View view, int w, int h,
                                                  int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioH == radioW) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = (int) (topMargin * radioH);
            }

        }
        container.addView(view, layoutParams);
    }


    public static void reSetPositionAndWH(ViewGroup container, View view, View newView,
                                          int w, int h, int leftMargin, int topMargin, Boolean isRoot) {
        if (view != null) {
            container.removeView(view);
        }

        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            layoutParams.topMargin = (int) (topMargin * radio_s);
        }
        container.addView(newView, layoutParams);
    }


    /**
     * 适配控件相关宽高
     *
     * @param container  父容器
     * @param view       控件
     * @param w          控件宽度
     * @param h          控件高度
     * @param leftMargin 以屏幕左上角为原点 的左边距
     * @param topMargin  以屏幕左上角为原点 的上边距
     * @param textSize   字体大小
     * @param isRoot     是否为根容器
     */
    public static void setPositionAndWH(ViewGroup container, View view, int w, int h,
                                        int leftMargin, int topMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            layoutParams.topMargin = (int) (topMargin * radio_s);
        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }


    /**
     * 适配控件相关宽高
     *
     * @param container  父容器
     * @param view       控件
     * @param w          控件宽度
     * @param h          控件高度
     * @param leftMargin 以屏幕左上角为原点 的左边距
     * @param topMargin  以屏幕左上角为原点 的上边距
     * @param textSize   字体大小
     * @param isRoot     是否为根容器
     */
    public static void setMsgPositionAndWH(ViewGroupcontainer, View view, int w, int h,int leftMargin, int topMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int)   (w * radio_s), (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin *    radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {

            if (radioH == radio) {
                layoutParams.leftMargin = (int) (leftMargin * radio_s);
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.leftMargin = (int) (leftMargin * radio_s);
                layoutParams.topMargin = (int) (topMargin * radio_s) + 50;
            }
        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }

    /**
     * 设置首页的大厅的玩家的信息的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param textSize
     * @param isRoot
     */
    public static void setNewHallPlayerInfoAndWH(ViewGroup container, View view, int w, int h,
                                                 int leftMargin, int topMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioW == radioH) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = (int) (topMargin * radio_s / 1.4);
            }

        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }

    /**
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param textSize
     * @param isRoot
     */
    public static void setWanrenBottomPlayerInfo(ViewGroup container, View view, int w, int h,
                                                 int leftMargin, int topMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            if (radioH == radioW) {
                layoutParams.topMargin = (int) (topMargin * radio_s);
            } else {
                layoutParams.topMargin = (int) (topMargin * radioH);
            }

        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }

    /**
     * 设置购物车中心的RadioButton的位置和大小
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param textSize
     * @param isRoot
     */
    public static void setShoppingCartRadioButtonAndWH(ViewGroup container, View view, int w, int h,
                                                       int leftMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }


    /**
     * 设置RadioButton的大小和位置
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param leftMargin
     * @param topMargin
     * @param textSize
     * @param isRoot
     */
    public static void setRadioButtonPositionAndWH(ViewGroup container, View view, int w, int h,
                                                   int leftMargin, int topMargin, int textSize, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s);
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            layoutParams.topMargin = (int) (topMargin * radio_s);

        }
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
                ((TextView) view).setTypeface(Typeface
                        .defaultFromStyle(Typeface.BOLD));
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        textSize * radio_s);
            }
        }
        container.addView(view, layoutParams);
    }

    /**
     * 初始化屏幕适配的相关参数  从大厅开始
     */
    public static void initScreenParam(Activity act) {
        DisplayMetrics dm = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
        sW = dm.widthPixels;
        sH = dm.heightPixels;
        Log.d("Base", "sW" + sW + "sH" + sH);
        radioH = sH / H;
        radioW = sW / W;
        if (radioH > radioW) {
            deltaY = (int) ((sH - radioW * H) / 2);
            radio_s = radioW;
        } else {
            deltaX = (int) ((sW - radioH * W) / 2);
            radio_s = radioH;
        }
    }

    /**
     * 判断系统版本是否高于3.0
     *
     * @return
     */
    public static boolean canPerformPropertyAnim() {
        try {
            int API_LEVEL = VERSION.SDK_INT;
            Log.i("ACT00", API_LEVEL + "");
            if (API_LEVEL >= 11) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    public static void setFloatPositionAndWH(ViewGroup container, View view,
                                             int w, int h, int leftMargin,
                                             float topMargin, Boolean isRight) {

        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams(
                (int) (w * BaseCommond.radio_s),
                (int) (h * BaseCommond.radio_s));
        if (isRight) {
            layoutParams.leftMargin = (int) (leftMargin * BaseCommond.radio_s) + BaseCommond.deltaX * 2;
            layoutParams.topMargin = (int) (topMargin * BaseCommond.radio_s) + BaseCommond.deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * BaseCommond.radio_s) + BaseCommond.deltaX;
            layoutParams.topMargin = (int) (topMargin * BaseCommond.radio_s) + BaseCommond.deltaY * 2;
        }
        container.addView(view, layoutParams);
    }

    /**
     *
     * @param container
     * @param view
     * @param w
     * @param h
     * @param isRight
     */
    public static void setViewPagerFloatPositionAndWH(ViewGroup container, View view, int w, int h, boolean isRight) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                (int) (w * BaseCommond.radio_s),
                (int) (h * BaseCommond.radio_s));
        if (!isRight) {
            layoutParams.rightMargin = (int) (20 * BaseCommond.radio_s);
        }
        container.addView(view, layoutParams);
    }

    /**
     *
     * @param container
     * @param view
     * @param w
     * @param h
     */
    public static void setLeftViewPagerFloatPositionAndWH(ViewGroup container, View view, int w, int h) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                (int) (w * BaseCommond.radio_s),
                (int) (h * BaseCommond.radio_s));
        layoutParams.rightMargin = (int) (20 * BaseCommond.radio_s);
        layoutParams.leftMargin = (int) (20 * BaseCommond.radio_s);
        container.addView(view, layoutParams);
    }

    public static void setStorePositionAndWH(ViewGroup container, View view, int w, int h, int left, int top, int textSize) {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                (int) (w * BaseCommond.radio_s),
                (int) (h * BaseCommond.radio_s));
        layoutParams.leftMargin = (int) (left * BaseCommond.radio_s);
        layoutParams.topMargin = (int) (top * BaseCommond.radio_s);
        if (textSize > 0) {
            if (view instanceof TextView) {
                ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * radio_s);
                ((TextView) view).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }
        container.addView(view, layoutParams);
    }
}

下面我对上面的类的核心的方法进行解读:

/**
     * 初始化屏幕适配的相关参数  从大厅开始
     */
    public static void initScreenParam(Activity act) {
        DisplayMetrics dm = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay()
                              .getMetrics(dm);
        sW = dm.widthPixels;
        sH = dm.heightPixels;
        Log.d("Base", "sW" + sW + "sH" + sH);
        radioH = sH / H;
        radioW = sW / W;
        if (radioH > radioW) {
            deltaY = (int) ((sH - radioW * H) / 2);
            radio_s = radioW;
        } else {
            deltaX = (int) ((sW - radioH * W) / 2);
            radio_s = radioH;
        }
    }

1.上面的方法获取手机的宽、高像素,然后跟指定为标准的宽、高进行比,求出其比例系数。取宽高比例系数较小的一个,为什么要取系数较小的一个呢?下面我们就来看看下面的图来帮助理解

带有虚拟的按键的手机的比例的计算

程序按照最小比例计算出来可能会产生手机屏幕底部留白的情况

程序调整之后的图片的位置

2.根据图很容易就能够就明白initScreenParam方法计算像素的比例的算法,我们总是取宽或者高的比例中较小的,采取极限的思维,如果去交大的值,那么必然会导致图片的宽或者高溢出屏幕的情况发生,这样图片显示就不完整。
2.这里的第二个核心的方法就是:

 /**
     * 适配控件相关宽高
     *
     * @param container  父容器
     * @param view       控件
     * @param w          控件宽度
     * @param h          控件高度
     * @param leftMargin 以屏幕左上角为原点 的左边距
     * @param topMargin  以屏幕左上角为原点 的上边距
     * @param isRoot     是否为根容器
     */
    public static void setPositionAndWH(ViewGroup container, View view, int w, int h,int leftMargin, int topMargin, Boolean isRoot) {
        FrameLayout.LayoutParams layoutParams;
        layoutParams = new FrameLayout.LayoutParams((int) (w * radio_s),
                (int) (h * radio_s));
        if (isRoot) {
            layoutParams.leftMargin = (int) (leftMargin * radio_s) + deltaX;
            layoutParams.topMargin = (int) (topMargin * radio_s) + deltaY;
        } else {
            layoutParams.leftMargin = (int) (leftMargin * radio_s);
            layoutParams.topMargin = (int) (topMargin * radio_s);
        }
        container.addView(view, layoutParams);
    }

**3.在这里,我们所有的布局都放在FrameLayout里面,所有的空间的位置都是相对于屏幕的左上角进行测量,然后按照比例进行计算其位置以及大小。我们关注的是这个函数的参数的含义,下面简单的介绍一下:
第一个参数是容器,这里就是我们定义的xml的FrameLayout,在我们的Activity的xml中的定义:**

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>

后面几个参数分别是控件的引用,控件的宽、高、控件离屏幕leftMargin和topMargin,使用的方法如下:

  //彩票的图标的大小和位置的设定
  ImageView ivLottery = new ImageView(this);
  ivLottery.setImageResource(R.drawable.lottery);
  ivLottery.setId(ROOM_LOTTERY);
  ivLottery.setOnClickListener(this);
  BaseCommand.setPositionAndWH(root, ivLottery, 112, 86, 874, 555, false);

4.上述的是用Java代码的方式实现简易的走势图的第一步,即创建在一个控制控件在屏幕上的大小和位置的基础类。

5具体如何的实现简易的走势图,敬请期待我的下一篇文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值