Android基础_创建自定义xml图片(二)

位图

bitmap
//1.在drawable文件中创建一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:tileMode="mirror"
    android:src="@drawable/p9"/>
<!--     tileMOdew= mirror /repeat平铺 -->
<!--     clamp,当不想拉伸图片当背景的时候
    会拉伸最后一个像素,所以需要最后一行/列像素为空白 -->
<!-- <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
        每个xml文件都有,给定一个命名空间用于区分xml,叫做android ,文件中所有的属性都带上了android-->

clip裁剪图片
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/p17"
    android:clipOrientation="vertical"
    android:gravity="top"
     >
</clip>
<!-- cliporientation指定裁剪的方向 
 gravity指定裁剪的起点 -->

selector
点击状态
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/p19" android:state_checked="true"/>
    <item android:drawable="@drawable/p18"/>
</selector>
点击效果
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!--         默认图片不需要状态,且默认图片需要位于最底层呢 -->
    <item android:drawable="@drawable/p6"
        android:state_pressed="true"
        />
    <item android:drawable="@drawable/p5"
        />
</selector>
进阶使用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
<!--         在levellist下 uselevel为true,其余用false -->
        <shape android:useLevel="false">
            <solid android:color="#8DEEEE"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item >
        <shape >
            <solid android:color="#6CA6CD"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>

</selector>

translation
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<!--     过渡图片 多个item -->
    <item 
     android:drawable="@android:color/transparent"
        />
    <item
        android:drawable="@drawable/p20"/>
</transition>
        ImageView iv = (ImageView) findViewById(R.id.iv);
//      获取src并强转
        TransitionDrawable td=(TransitionDrawable) iv.getDrawable();
//      进行切换操作 durationmillsi 图片切换的时间
        td.startTransition(2000);

level-list
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--     其本质是有setlevel(2) 
        maxlevel= (0,2]
     -->
    <item android:drawable="@drawable/pp1" android:maxLevel="0"/>
    <item android:drawable="@drawable/pp2" android:maxLevel="1"/>
    <item android:drawable="@drawable/pp3" android:maxLevel="2"/>
    <item android:drawable="@drawable/pp4" android:maxLevel="3"/>
    <item android:drawable="@drawable/pp5" android:maxLevel="4"/>
</level-list>
点击事件
            LevelListDrawable drawable = (LevelListDrawable) mIv.getDrawable();
            if(drawable.getLevel()>4){
                drawable.setLevel(0);
            }else {

                drawable.setLevel(drawable.getLevel()+1);

inset
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/p15"
    android:insetTop="20dp" >
</inset>

ninePatch:
自定义图片被拉伸的像素点
当图片被拉伸时,只有设置的像素点会被拉伸
常用于聊天APP的气泡效果

shape
shape分割线
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    >
    <size 
        android:height="0.5dp"/>
    <solid 
        android:color="#999"/>
    <padding android:right="20dp"
        android:left="20dp"/>
</shape>
view分割线 
        <View 
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="0.5dp"
            android:layout_margin="10dp"
            android:background="#888"/>
创建圆环图片
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"
    android:innerRadius="30dp"
    android:thickness="20dp"
    android:useLevel="false">
<!--     shape代表当前定义的形状
    innerRadius内部半径
    thickness厚度
    uselevel是否在levellistdrawable中使用
    innerradisuratio内部半径的比例
    thicknessratio厚度的比例-->
<!--    定义一个颜色渐变效果
    angle 从哪个角度开始 默认45角
    type渐变类型 sweep扫过-->
    <gradient 
        android:startColor="#F00"
        android:centerColor="#0F0"
        android:endColor="#00f"
        android:angle="0"
        android:type="sweep"
        />
<!--    定义边界 -->
    <stroke 
        android:width="1dp"
        android:color="@android:color/transparent"/>
<!--    设置图形的大小 -->
    <size 
        android:width="122dp"
        android:height="122dp"/>
</shape>
自定义EditText的圆角矩形背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
<!--     默认矩形 -->
<!--    定义圆角
        radius 4个圆角的半径
     -->
    <corners 
        android:radius="5dp"/>
<!--    定义填充颜色(单色) -->
    <solid 
        android:color="#444"/>
<!--    定义边界 -->
    <stroke 
        android:width="1dp"
        android:color="@android:color/black"/>
<!--    定义padding -->
    <padding 
        android:top="5dp"
        android:bottom="5dp"
        android:left="5dp"/>
</shape>

自定义进度条样式
    <ProgressBar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="40"
        android:layout_margin="10dp"
        android:progressDrawable="@drawable/my_layerlist_progress"
        />
 my_layerlist_progress:       
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--     id绑定 背景图要在最前面 
    轮廓会依照背景图裁剪-->
    <item android:drawable="@drawable/progress_bar_bg" 
        android:id="@android:id/background"/>
    <item android:drawable="@drawable/progress_bar_selected_bg"
        android:id="@android:id/progress"/>
</layer-list>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建一个自定义 XML 弹窗,可以按照以下步骤进行: 1. 创建一个 XML 布局文件,定义你想要的弹窗视图。例如,你可以创建一个包含文本框、按钮等控件的布局文件。假设你的文件名为 `custom_dialog.xml`。 2. 在你的 Activity 中,创建一个 AlertDialog.Builder 对象,并调用其 `setView()` 方法来设置弹窗的视图。例如: ```java AlertDialog.Builder builder = new AlertDialog.Builder(this); View view = getLayoutInflater().inflate(R.layout.custom_dialog, null); builder.setView(view); ``` 这里的 `getLayoutInflater().inflate(R.layout.custom_dialog, null)` 方法用于将 `custom_dialog.xml` 文件转化为一个 View 对象。 3. 可以通过调用 AlertDialog.Builder 的其他方法来设置弹窗的属性,例如标题、按钮等。例如: ```java builder.setTitle("自定义弹窗"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理确定按钮的点击事件 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理取消按钮的点击事件 } }); ``` 4. 调用 AlertDialog.Builder 的 `create()` 方法来创建 AlertDialog 对象,并显示出来。例如: ```java AlertDialog dialog = builder.create(); dialog.show(); ``` 这样就可以在你的 Android 应用中创建一个自定义 XML 弹窗了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值