Android 高级图形图像处理

这篇博客详细介绍了在Android中实现高级图形图像处理的各种技术,包括绘制各种形状、渐变效果、图像裁剪、动画效果等。通过示例代码展示了如何使用shape文件和XML布局实现线性、扫描、放射状梯度渐变,以及如何运用属性动画实现透明、旋转、位移等效果。此外,还涵盖了拖曳管控、横竖屏切换视图适配、自定义View的尺寸和位置控制,以及状态栏消息通知的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 绘制矩形、扇形、圆角矩形

// activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >
    <TextView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:id="@+id/textview1" />
    <TextView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:id="@+id/textview2"/>
    <TextView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:id="@+id/textview3"/>
    <TextView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:id="@+id/textview4"/>

</LinearLayout>
//main.java
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //圆角矩形
        //PaintDrawable继承自ShapeDrawable
        PaintDrawable drawable1=new PaintDrawable(Color.GREEN);
        drawable1.setCornerRadius(60);//圆角弧度
        findViewById(R.id.textview1).setBackgroundDrawable(drawable1);

        //椭圆
        OvalShape ovalShape = new OvalShape();
        ShapeDrawable drawable2=new ShapeDrawable(ovalShape);
        drawable2.getPaint().setColor(Color.BLUE);
        drawable2.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
        findViewById(R.id.textview2).setBackgroundDrawable(drawable2);

        //矩形
        RectShape rectShape =new RectShape();
        ShapeDrawable drawable3=new ShapeDrawable(rectShape);
        drawable3.getPaint().setColor(Color.RED);
        drawable3.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
        findViewById(R.id.textview3).setBackgroundDrawable(drawable3);

        //弧度扇形
        ArcShape arcShape = new ArcShape(100,120);//顺时针开始角度100°, 绘制区域为120°
        ShapeDrawable drawable4=new ShapeDrawable(arcShape);
        drawable4.getPaint().setColor(Color.BLACK);
        drawable4.getPaint().setStyle(Paint.Style.FILL);//填充形式为完全填充
        findViewById(R.id.textview4).setBackgroundDrawable(drawable4);

    }

2.用xml代码布局文件实现复杂的图像

IDE: Android studio
【1】创建Shape文件:
	(1)	右击app->New->Android resource file
	(2)	File name: 填写Shape文件的名字(如 gradient)
	(3)	Resource type: Drawable
	(4)	Root element: shape
	(5)	Shape文件中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"> // 定义一个椭圆

    <gradient
        android:angle="45"	
        android:centerColor="@android:color/holo_orange_dark"
        android:endColor="@android:color/holo_blue_dark"
        android:startColor="@android:color/holo_red_dark" />

    <stroke	//描边
        android:width="10dip"		//所描边的厚度
        android:color="@android:color/black"		
        android:dashGap="50dip"	//生成不连续间断的长度
        android:dashWidth="111dip"		//黑色断片的长度
        />

</shape>

【2】在布局文件activity_main.xml中加入
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/image_view"
        android:layout_centerInParent="true"
        android:background="@drawable/gradient"
        />

</RelativeLayout>

【3】java代码中只需
setContentView(R.layout.activity_main);


3.圆环形渐变

shape文件代码如下(替换实例2的shap文件)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="20dp"
    android:useLevel="false">

    <gradient
        android:angle="90"
        android:centerColor="@android:color/holo_orange_dark"
        android:endColor="@android:color/holo_blue_dark"
        android:startColor="@android:color/holo_red_dark"/>

    <stroke
        android:width="5dp"
        android:color="@android:color/black"/>


</shape>

4.矩形渐变

shape文件如下(替换实例2的shap文件)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="90"
        android:centerColor="@android:color/holo_orange_dark"
        android:endColor="@android:color/holo_blue_dark"
        android:startColor="@android:color/holo_red_dark"/>

    <stroke
        android:width="5dp"
        android:color="@android:color/black"/>

</shape>

5.渐变线

shape文件如下(替换实例2的shap文件)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:centerColor="@android:color/holo_blue_light"
        android:endColor="@android:color/transparent"     //transparent为黑色全透明
        android:startColor="@android:color/transparent"/>

    <size android:height="1px"/>

</shape>

6.动态渐变

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Drawable[] drawables=new Drawable[]{new ColorDrawable(Color.TRANSPARENT),new ColorDrawable(Color.RED)};

        TransitionDrawable mTransitionDrawable=new TransitionDrawable(drawables);

        ImageView image=(ImageView)findViewById(R.id.image_view);
        image.setImageDrawable(mTransitionDrawable);

        mTransitionDrawable.setCrossFadeEnabled(true);
        mTransitionDrawable.startTransition(10000);
        
    }

7.对图片进行裁剪

#正常图像
        ImageView image1=(ImageView) findViewById(R.id.image_view1);
        image1.setImageResource(R.drawable.yes);

#矩形四角磨成圆脚
        RoundedBitmapDrawable round= RoundedBitmapDrawableFactory.create(getResources()
        , BitmapFactory.decodeResource(getResources(),R.drawable.yes));
        round.getPaint().setAntiAlias(true);
        round.setCornerRadius(30);
        ImageView image2=(ImageView) findViewById(R.id.image_view2);
        image2.setImageDrawable(round);

#裁剪为圆形图像
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.yes);
        RoundedBitmapDrawable circle= RoundedBitmapDrawableFactory.create(getResources()
                , BitmapFactory.decodeResource(getResources(),R.drawable.yes));
        circle.getPaint().setAntiAlias(true);
        circle.setCornerRadius(Math.max(bitmap.getWidth(),bitmap.getHeight()));
        ImageView image3=(ImageView) findViewById(R.id.image_view3);
        image3.setImageDrawable(circle);

8.灵活的调用图片在Imageview显示

p.xml(在drawable中)代码如下:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="1" android:drawable="@drawable/a"/>
    <item android:maxLevel="2" android:drawable="@drawable/b"/>
    <item android:maxLevel="3" android:drawable="@drawable/c"/>

</level-list>

*.java

        ImageView image1=(ImageView) findViewById(R.id.image_view1);
        image1.setImageResource(R.drawable.p);
        image1.setImageLevel(3);

9.红色小圆球样式的新消息提醒

*.java

setContentView(R.layout.activity_main);

shape文件(round.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--填充颜色值-->
    <solid android:color="#FFA500"/>
    <!--radius值越大,越趋于圆形-->
    <corners android:radius="5dip" />
    <!--圆角图像内部填充四周的大小,将会已此挤压内部布置的view-->
    <padding
        android:bottom="3dip"
        android:left="3dip"
        android:right="3dip"
        android:top="3dip"/>
</shape>

shape文件(tips_circle.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="#FF0000"/>

</shape>

布局文件

<?xml version="1.0" encoding="utf-8"?>
<!--(帧布局)
android:layout_m
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值