wangy 发表于 2021-7-1 08:42:33

【lua教程】简易选色器

require "import"
import "android.widget.*"
import "android.view.*"
import "android.graphics.PorterDuffColorFilter"
import "android.graphics.PorterDuff"
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
取色器=
{
LinearLayout;
orientation="vertical";
layout_width="fill";
layout_height="fill";
gravity="center";
{
    CardView;
    id="卡片图";
    layout_margin="10dp";
    radius="40dp",
    elevation="0dp",
    layout_width="20%w";
    layout_height="20%w";
};
{
    TextView;
    layout_margin="0dp";
    textSize="12sp";
    id="颜色文本";
    textColor=左侧栏项目色;
};
{
    SeekBar;
    id="拖动一";
    layout_margin="15dp";
    layout_width="match";
    layout_height="wrap";
};
{
    SeekBar;
    id="拖动二";
    layout_margin="15dp";
    layout_width="match";
    layout_height="wrap";
};
{
    SeekBar;
    id="拖动三";
    layout_margin="15dp";
    layout_width="match";
    layout_height="wrap";
};
{
    SeekBar;
    id="拖动四";
    layout_margin="15dp";
    layout_width="match";
    layout_height="wrap";
};
};
--对话框View
local 取色器=loadlayout(取色器)
拖动一.setMax(255)
拖动二.setMax(255)
拖动三.setMax(255)
拖动四.setMax(255)
拖动一.setProgress(0xff)
拖动二.setProgress(0x1e)
拖动三.setProgress(0x8a)
拖动四.setProgress(0xe8)
--监听
拖动一.setOnSeekBarChangeListener{
onProgressChanged=function(view, i)
    updateArgb()
end
}

拖动二.setOnSeekBarChangeListener{
onProgressChanged=function(view, i)
    updateArgb()
end
}

拖动三.setOnSeekBarChangeListener{
onProgressChanged=function(view, i)
    updateArgb()
end
}

拖动四.setOnSeekBarChangeListener{
onProgressChanged=function(view, i)
    updateArgb()
end
}
--更新颜色
function updateArgb()
local a=拖动一.getProgress()
local r=拖动二.getProgress()
local g=拖动三.getProgress()
local b=拖动四.getProgress()
local argb_hex=(a<<24|r<<16|g<<8|b)
颜色文本.Text=string.format("%#x", argb_hex)
卡片图.setCardBackgroundColor(argb_hex)
end
--翻译进度
argbBuild=AlertDialog.Builder(activity)
argbBuild.setView(取色器)
argbBuild.setTitle("选色器")
argbBuild.setPositiveButton("复制", {
onClick=function(view)
    local a=拖动一.getProgress()
    local r=拖动二.getProgress()
    local g=拖动三.getProgress()
    local b=拖动四.getProgress()
    local argb_hex=(a<<24|r<<16|g<<8|b)
    local argb_str=string.format("%#x", argb_hex)
    activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(argb_str)
    print("已复制到剪贴板")
end
})
argbBuild.setNeutralButton("取消",{onClick=function()

end})--设置否认按钮
--实例化对话框
argbDialog=argbBuild.create()
argbDialog.setCanceledOnTouchOutside(false)
function showArgbDialog()
--展示对话框
argbDialog.show()
--更新颜色
updateArgb()
end
showArgbDialog()
页: [1]
查看完整版本: 【lua教程】简易选色器