【lua教程】自绘制开关
require "import"import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.graphics.drawable.GradientDrawable"
activity.setTitle('AndroLua+')
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
activity.setContentView(loadlayout({
LinearLayout;
gravity="center";
layout_height="fill";
layout_width="fill";
orientation="vertical";
--BackgroundColor="#ffffff00",
id="fhhk";
{
Switch;
id="Grt";
};
{
Switch;
id="Grt1";
};
{
Switch;
id="Grt2";
};
{
Switch;
id="Grt3";
};
{
Switch;
};
}))
--转换像素单位
dip={toPx=function(context, dpValue)
scale = context.getResources().getDisplayMetrics().density;
return dpValue * scale + 0.5
end}
function CircleBack_SeekDra(InsideColor,Ad_Size,ble,Colorse3)
local colors = InsideColor
local Sizes=dip.toPx(this,Ad_Size)--设置开关大小
local Stroke=dip.toPx(this,7)--设置拖块边距
local Track_Stroke=dip.toPx(this,0)--设置背景边距
local drawable = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,{});
drawable.setCornerRadius(Sizes/2);
drawable.setColor(InsideColor)
if ble
drawable.setStroke(Stroke, 0x00ffffff)
drawable.setSize(Sizes,Sizes)
else
drawable.setAlpha(60)
drawable.setStroke(Track_Stroke, 0x00ffffff)
end
drawable.setGradientType(GradientDrawable.RECTANGLE);
return drawable
end
Switch_x=function(view,Colors,Colors2,Colors3,Ad_Size)
pcall(function()
if view.isChecked()
Colorse=Colors
Colorse2=Colors
else
Colorse=Colors2
Colorse2=Colors3
end
local padd_W=dip.toPx(this,Ad_Size/2.5)
view.setThumbDrawable(CircleBack_SeekDra(Colorse,Ad_Size,true))
.setTrackDrawable(CircleBack_SeekDra(Colorse2,Ad_Size,false))
.setPadding(padd_W,padd_W,padd_W,padd_W)
end)
end
Grt.setOnCheckedChangeListener({
onCheckedChanged=function(buttonView, isChecked)
Switch_x(Grt,Ad_Color,Ad_Color2,Ad_Color3,Ad_Size,Ad_Padding)--更新状态
end})
Grt1.setOnCheckedChangeListener({
onCheckedChanged=function(buttonView, isChecked)
Switch_x(Grt1,0xffff6e17,0xFFECECEC,0xff000000,35,7)--预加载
end})
Grt2.setOnCheckedChangeListener({
onCheckedChanged=function(buttonView, isChecked)
Switch_x(Grt2,0xff2ecbff,0xFFECECEC,0xff000000,45,7)--预加载
end})
Grt3.setOnCheckedChangeListener({
onCheckedChanged=function(buttonView, isChecked)
Switch_x(Grt3,0xffae5dff,0xFFECECEC,0xff000000,55,7)--预加载
end})
Ad_Color=0xffff0000--边框颜色与拖块选中颜色
Ad_Color3=0xff000000--设置底部背景未选中颜色
Ad_Color2=0xFFECECEC
Ad_Size=25--按钮的大小dip
Ad_Padding=7--设置拖块边距范围dip
Switch_x(Grt,Ad_Color,Ad_Color2,Ad_Color3,Ad_Size,Ad_Padding)--预加载
Switch_x(Grt1,0xffff6e17,0xFFECECEC,0xff000000,35,7)--预加载
Switch_x(Grt2,0xff2ecbff,0xFFECECEC,0xff000000,45,7)--预加载
Switch_x(Grt3,0xffae5dff,0xFFECECEC,0xff000000,55,7)--预加载
来源:代码手册 Robin
页:
[1]