美文网首页
安卓屏幕适配插件----Android Studio插件编写

安卓屏幕适配插件----Android Studio插件编写

作者: 逍遥才子 | 来源:发表于2019-06-25 16:46 被阅读0次

    插件原理:使用intellij提供的platformsdk开发Android Studio插件,实现在编写xml布局文件时,利用快捷键生成不同分辨率下的dimen文件条目的插件


    image.png

    MakeUi2View:

    package com.myhuanghai.ui2view;
    
    import com.intellij.openapi.actionSystem.AnAction;
    import com.intellij.openapi.actionSystem.AnActionEvent;
    import com.intellij.openapi.actionSystem.PlatformDataKeys;
    import com.intellij.openapi.editor.Editor;
    import com.intellij.openapi.editor.SelectionModel;
    import com.intellij.openapi.ui.MessageType;
    import com.intellij.openapi.ui.Messages;
    import com.intellij.openapi.ui.popup.Balloon;
    import com.intellij.openapi.ui.popup.JBPopupFactory;
    import com.intellij.openapi.wm.StatusBar;
    import com.intellij.openapi.wm.WindowManager;
    import com.intellij.ui.awt.RelativePoint;
    import org.apache.http.util.TextUtils;
    
    public class MakeUi2View extends AnAction {
    
        @Override
        public void actionPerformed(AnActionEvent e) {
            Editor editor = e.getData(PlatformDataKeys.EDITOR);
            if (editor == null)
                return;
            SelectionModel selectionModel = editor.getSelectionModel();
            //获取选中内容
            final String selectedText = selectionModel.getSelectedText();
            if (TextUtils.isEmpty(selectedText)) {
                //Utils.showNotification(project, MessageType.ERROR,"请选中生成内容");
                StatusBar statusBar = WindowManager.getInstance().getStatusBar(e.getProject());
    
                JBPopupFactory.getInstance()
                        .createHtmlTextBalloonBuilder("请选中生成内容", MessageType.ERROR, null)
                        .setFadeoutTime(7500)
                        .createBalloon()
                        .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight);
                return;
            }
            int i = 0;
            try {
                i = Integer.parseInt(selectionModel.getSelectedText().replace("x", ""));
            } catch (Exception e1) {
                return;
            }
    
            DimenTool.Davn(e.getProject(), i);
            Messages.showMessageDialog("生成成功:@dimen/" + selectionModel.getSelectedText(), "hello", Messages.getInformationIcon());
    
        }
    }
    
    

    DimenTool:

    package com.myhuanghai.ui2view;
    
    import com.intellij.openapi.project.Project;
    
    import java.io.*;
    
    public class DimenTool {
    
    
        public static float defaultDp = 360f;
        public static float uippi = 72f;
        public static float uiwidth = 750f;
    
    
        private static final String[] defaultDPArr = new String[]{"384", "392", "400", "410",
                "411", "450", "480", "533",
                "540", "592", "600", "640",
                "662", "720", "768", "800",
                "811", "820", "960", "961",
                "1024", "1280", "1365"};
    
        public static int getPx(int px) {
            // float f = uippi/160f;//1dp = f px
            // int dp = (int)((float)px/f);//目标像素在ui设备屏幕有多少dp
            float dp = (float) px / uiwidth * defaultDp;
            //int dp = (int)((float)px/uippi*160f) ;
            return (int) dp;
        }
    
        public static void Davn(Project project, int px) {
    
           /* File file = new File("./resources/1.txt");
            BufferedReader bis = null;
            String s = null;
            try {
                bis = new BufferedReader(new FileReader(file));
                int index = 0;
                while (null != (s = bis.readLine())) {
                    if (index == 0)
                        defaultDp = Integer.parseInt(s);
                    else if (index == 1) {
                        uippi = Integer.parseInt(s);
                    } else if (index == 2) {
                        uiwidth = Integer.parseInt(s);
                    }
                    index++;
                }
                s = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                bis = null;
            }*/
            String path = project.getBasePath() + "/";
    
            StringBuilder defsb = new StringBuilder();
    
            File def = new File(path + "app/src/main/res/values/dimens.xml");
            if (!def.exists()) {
                Make(def);
                defsb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
                        "<resources>\r\n" + "</resources>");
    
            }
            //defsb.append("\t").append("<dimen name=\"x" + px + "\">" + (getPx(px) *360/ defaultDp) + "dp</dimen>").append("\r\n").append("</resources>");
            defsb.append("\t").append("<dimen name=\"x" + px + "\">" + (getPx(px) * 360 / defaultDp) + "dp</dimen>").append("\r\n").append("</resources>");
            writeFile(def, defsb.toString());
    
            for (int x=0;x<defaultDPArr.length;x++){
                StringBuilder stringBuilder = new StringBuilder();
    
                File file1 = new File(path + "app/src/main/res/values-w"+defaultDPArr[x]+"dp/dimens.xml");
                if (!file1.exists()) {
                    Make(file1);
                    stringBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
                            "<resources>\r\n" + "</resources>");
    
                }
                stringBuilder.append("\t").append("<dimen name=\"x" + px + "\">" + (getPx(px) * Integer.parseInt(defaultDPArr[x]) / defaultDp) + "dp</dimen>").append("\r\n").append("</resources>");
                writeFile(file1, stringBuilder.toString());
            }
            try {
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
    
            }
    
        }
    
        private static void rep(String ch, File file) {
            BufferedReader bis = null;
            FileWriter bos = null;
            String s = null;
            File file1 = new File(file.getPath() + "1");
            try {
                bis = new BufferedReader(new FileReader(file));
    
                bos = new FileWriter(file1);
                while (null != (s = bis.readLine())) {
    
    
                    s = s.replaceAll(ch, "");
                    if (s.equals(""))
                        continue;
                    s = s + "\r\n";
                    System.out.println(s);
                    bos.write(s);
                }
                bos.close();
                bis.close();
                s = null;
                bis = null;
                bos = null;
                BufferedReader bis1 = null;
                FileWriter bos1 = null;
                bis1 = new BufferedReader(new FileReader(file1));
                bos1 = new FileWriter(file);
                while (null != (s = bis1.readLine())) {
                    s = s + "\r\n";
                    bos1.write(s);
                }
    
                bos1.close();
                bis1.close();
                s = null;
                file1.delete();
                bis1 = null;
                bos1 = null;
    
            } catch (FileNotFoundException e) {
                System.out.println("未找到文件\n");
            } catch (IOException ee) {
                System.out.println("aaa");
            }
    
    
        }
    
        /**
         * 写入方法
         */
    
        public static void writeFile(File file, String text) {
    
            PrintWriter out = null;
            FileWriter fw = null;
    
            try {
    
                fw = new FileWriter(file, true);
                out = new PrintWriter(fw);
                out.println(text);
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            } finally {
                out.close();
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fw = null;
                out = null;
            }
    
    
            rep("</resources>", file);
    
            try {
                fw = new FileWriter(file, true);
                out = new PrintWriter(fw);
                out.println("</resources>");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out = null;
                fw = null;
            }
    
    
        }
    
        //自定义检测生成指定文件夹下的指定文件
        public static void Make(File file) {
            try {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:安卓屏幕适配插件----Android Studio插件编写

          本文链接:https://www.haomeiwen.com/subject/cjrxcctx.html