首页 > 其他分享 >android 插件编写

android 插件编写

时间:2023-08-23 14:00:47浏览次数:36  
标签:插件 intellij void new bitmap import 编写 android com

android插件编写一般用IntelliJ IDEA 基本的插件编写流程 不做赘述,

本文主要记录编写插件的UI交互弹框,以及在指定目录下生成具体的类,并且将指定的代码写入到类文件里

1、首选创建对应的Action类如 SecondActionClassName这个类 并将其注册到plugin.xml中,设置其触发的为位置以及快捷键

 SecondActionClassName具体的代码如下:

package com.testdemo.example;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;

import java.io.IOException;

public class SecondActionClassName extends AnAction {

    String templateString = "import android.content.Context\n" +
            "import android.graphics.Bitmap\n" +
            "import android.graphics.Canvas\n" +
            "import android.graphics.Paint\n" +
            "import android.graphics.pdf.PdfRenderer\n" +
            "import android.view.ViewGroup\n" +
            "import com.github.chrisbanes.photoview.PhotoView\n" +
            "import com.module.base.R\n" +
            "import com.module.platform.utils.getColor\n" +
            "import es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter\n" +
            "\n" +
            "class PdfViewerAdapter(context: Context, pdfPath: String?) : BasePDFPagerAdapter(context, pdfPath) {\n" +
            "\n" +
            "    override fun instantiateItem(container: ViewGroup, position: Int): Any {\n" +
            "        val photoView = PhotoView(context)\n" +
            "        if (renderer == null || count < position) {\n" +
            "            return photoView\n" +
            "        }\n" +
            "        val page = getPDFPage(renderer, position)\n" +
            "\n" +
            "        var bitmap = bitmapContainer[position]\n" +
            "        bitmap?.let {\n" +
            "            bitmap = drawBitmapBg(getColor(R.color.C_FFFFFF), bitmap)\n" +
            "        }\n" +
            "        page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)\n" +
            "        page.close()\n" +
            "\n" +
            "        photoView.setImageBitmap(bitmap)\n" +
            "        container.addView(\n" +
            "            photoView,\n" +
            "            0,\n" +
            "            ViewGroup.LayoutParams(\n" +
            "                ViewGroup.LayoutParams.MATCH_PARENT,\n" +
            "                ViewGroup.LayoutParams.MATCH_PARENT\n" +
            "            )\n" +
            "        )\n" +
            "        return photoView\n" +
            "    }\n" +
            "\n" +
            "    fun drawBitmapBg(color: Int, originBitmap: Bitmap): Bitmap? {\n" +
            "        val paint = Paint()\n" +
            "        paint.color = color\n" +
            "        val bitmap =\n" +
            "            Bitmap.createBitmap(originBitmap.width, originBitmap.height, originBitmap.config)\n" +
            "        val canvas = Canvas(bitmap)\n" +
            "        canvas.drawRect(0f, 0f, originBitmap.width.toFloat(), originBitmap.height.toFloat(), paint)\n" +
            "        canvas.drawBitmap(originBitmap, 0f, 0f, paint)\n" +
            "        return bitmap\n" +
            "    }\n" +
            "}";
    @Override
    public void actionPerformed(AnActionEvent e) {

        //调起测试弹框 并设置了测试弹框点击OK按钮时的回调,其中可以回调用户在测试弹框里的输入内容
        TestShowDialog dialog = new TestShowDialog(str -> {
            //当我们想要向文件写入代码的时候需要调用此异步方法
            WriteCommandAction.runWriteCommandAction(e.getProject(), () -> {
                //获取当前鼠标所选的文件目录
                PsiDirectory chooseDirectory = e.getRequiredData(LangDataKeys.IDE_VIEW).getOrChooseDirectory();
                if (chooseDirectory != null) {
                    //在此目录下创建具体的文件
                    PsiFile file = chooseDirectory.createFile("TestClass.kt");
                    VirtualFile virtualFile = file.getVirtualFile();
                    try {
                        //向文件写入具体的代码
                        virtualFile.setBinaryContent(templateString.getBytes());
                    } catch (IOException ex) {
                        //这个是个message弹框
                        Messages.showMessageDialog("errrrrrr", "tillle22222: " + str, Messages.getInformationIcon());
                        throw new RuntimeException(ex);
                    }
                }

                PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
                PsiElement psiElement = e.getData(LangDataKeys.PSI_ELEMENT);
                //此方法可以获取当前文件的Editor对象 有了
                Editor editor = e.getData(CommonDataKeys.EDITOR);
                if (editor != null) {
                    Document document = editor.getDocument();
                    //向文件的指定位置插入具体的代码
                    document.insertString(0, "12345qwert\nmnbv\n" + str);
                    //这个是个message弹框
                    Messages.showMessageDialog("titlllle", "tillle22222: " + str, Messages.getInformationIcon());
                }
            });
        });
        dialog.setSize(500, 500);
        dialog.setVisible(true);
    }

}

actionPerformed方法在用户触发插件的入口时会被调用 ,通过以上代码就完成了在鼠标选中指定目录 然后触发插件弹出弹框,

并获取用户输入的内容,去创建对应文件并写入代码到具体文件 

弹框的代码如下:

 

package com.testdemo.example;

import javax.swing.*;
import java.awt.event.*;

public class TestShowDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;

    private JTextField tfUserName;

    private JLabel lbUserName;

    interface CustomClickListener {
        void onConfirmClick(String str);
    }

    private CustomClickListener mCustomClickListener;

    public TestShowDialog(CustomClickListener customClickListener) {
        mCustomClickListener = customClickListener;
        createUIComponents();
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        String inputStr = tfUserName.getText();
        if (mCustomClickListener != null) {
            mCustomClickListener.onConfirmClick(inputStr);
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    private void createUIComponents() {
        contentPane = new JPanel();
        lbUserName = new JLabel();
        lbUserName.setText("input userName: ");
        tfUserName = new JTextField();
        tfUserName.setText("user_name");
        lbUserName.setBounds(30, 30, 100, 50);
        tfUserName.setBounds(200, 30, 200, 50);
        contentPane.add(lbUserName);
        contentPane.add(tfUserName);

        buttonOK = new JButton("ok");
        buttonCancel = new JButton("cancel");
        buttonOK.setBounds(100, 460, 100, 50);
        buttonCancel.setBounds(350, 460, 100, 50);
        contentPane.add(buttonOK);
        contentPane.add(buttonCancel);
        contentPane.setBounds(100, 100, 300, 300);
    }
}

 

标签:插件,intellij,void,new,bitmap,import,编写,android,com
From: https://www.cnblogs.com/bimingcong/p/17651434.html

相关文章

  • idea安装bigdata tool 插件 HDFS客户端
    在idea里面下载bigdatatool插件使用hdfs客户端一、下载hadoop安装包,并将文件解压二、在idea的插件里面找到bbigdatatool插件并进行安装 如果在idea里面下载比较慢,可以在官网下载 BigDataTools-IntelliJIDEsPlugin|Marketplace(jetbrains.com) 在idea设置里面......
  • Android入门教程之Activity(生命周期,启动...)
    Activity是一个应用组件,用户可与其提供的屏幕进行交互,以执行拨打电话、拍摄照片、发送电子邮件或查看地图等操作。每个Activity都会获得一个用于绘制其用户界面的窗口。窗口通常会充满屏幕,但也可小于屏幕并浮动在其他窗口之上。Activity1.Activity的使用我们新建的工程中带有......
  • Android面试:加班才能将事情做到最好?我不配
    昨天,我面试了一家公司,价值观的冲突让我感到不安。面试官在技术方面非常出色,他可能是一个完美主义者,无论面对什么问题,他似乎都能找到解决方案。我被他的能力所震撼,感到有些无所适从。然而,我无法认同的是,面试官将加班视为理所当然。他甚至表示,有些人希望将事情做得更好,自愿加班。这个......
  • Android并发编程高级面试题汇总(含详细解析 七)
    Android并发编程高级面试题汇总最全最细面试题讲解持续更新中......
  • 6款程序员必备的 Chrome 扩展插件!逼格秒提升
    美化JustBlack午夜黑主题简介:Chrome官方团队出品的黑色主题皮肤,值得拥有!推荐指数:⭐⭐⭐⭐⭐下载链接:https://www.chajianxw.com/themes/18893.htmlDarkReader暗黑主题简介:能在任何网站上开启夜间模式,同时,它还支持自定义调整亮度、对比度,应用棕褐色滤镜、黑暗模式,设置字体和忽......
  • Pybind11:使用C++编写Python模块
    放假摆了一周了。看论文实在不是什么有意思的活。这两天研究了一下Pybind11的用法。使用C/C++和Python混合编程的想法很早就有了,在大一的一次比赛时曾经实践过(虽然不是我写的),当时获得了比较显著的性能提升。但是当时用的是Swig,据队友说Swig对于NumPy的支持极为阴间,当时调试花了好......
  • 11 款程序员必备的 Chrome 扩展插件! 提升开发效率!
    Octotree树形菜单简介:Octotree是一款增强GitHub代码审查和探索的浏览器扩展,可以非常方便帮助您查阅代码。推荐指数:⭐⭐⭐⭐⭐下载链接:https://www.chajianxw.com/developer/11032.html划词翻译简介:一站式划词/截图/网页全文/音视频翻译扩展,支持谷歌、DeepL、百度、搜狗等......
  • 继copilot之后,又一款免费帮你写代码的插件CodeGeeX
    合集-开发工具(6)  1.Weblogic11g安装部署-winserver篇05-072.给你安利一款国产良心软件uTools05-133.gitee图床不能用了,心态崩了05-164.windows环境下如何优雅搭建ftp服务?05-175.IntelliJIDEA上手这一篇就够了,从入门到上瘾05-226.继copilot之后,又一款免费帮你写代......
  • 继copilot之后,又一款免费帮你写代码的插件
    写在前面在之前的文章中推荐过一款你写注释,它就能帮你写代码的插件copilotcopilot写代码的能力没得说,但是呢copilot试用没几天之后就收费了传送门:你写注释她帮你写代码按理说这么好用,又可以提高效率的工具,收点费也理所当然但是秉承白嫖一时爽,一直白嫖一直爽的原则(主要是我穷......
  • 三维脚本插件
    新建两个纯色层新建一个摄像机新建一个摄像机然后把面分开通过旋转,平移,能把面搭建出来先用文字平铺形成一个面然后把这个面预合成复制六个面打开脚本注意不能直接导入脚本,要文件-脚本-运行脚本新建一个摄像机观察设置空对象,方便观察......