Graphics简单汇总

2024-08-31 23:58
文章标签 简单 汇总 graphics

本文主要是介绍Graphics简单汇总,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、主页面布局文件


activity_main.xml(只有2个button按钮)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="testTuPian"android:text="测试图片处理" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="testDraw"android:text="测试绘制图形" />
</LinearLayout>

MainActivity.java(启动2个button)

package com.atguigu.l11_graphics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void testTuPian(View view) {startActivity(new Intent(this, TuPianTestActivity.class));}public void testDraw(View view) {startActivity(new Intent(this, DrawTestActivity.class));}
}

2、startActivity(new Intent(this, TuPianTestActivity.class));启动的界面


上图布局文件如下

activity_tupian_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick=<span style="color:#ff0000;">"testBD"</span>android:text="测试Bitmap" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="<span style="color:#ff0000;">testMatrix</span>"android:text="测试图片的缩放等处理" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="使用Shape做的按钮" android:background="@drawable/shape_test"/><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:background="@drawable/image_selector" android:onClick="<span style="color:#ff0000;">clickIV</span>"/><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="使用Selector+Shape做的按钮"android:background="@drawable/shape_selector"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/test2"android:text="A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. A NinePatch drawable is a standard PNG image that includes an extra" />
</LinearLayout>
TuPianTestActivity.java

package com.atguigu.l11_graphics;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/** 测试操作图片的Activity*/
public class TuPianTestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tupian_test);}public void<span style="color:#ff0000;"> testBD</span>(View v) {startActivity(new Intent(this, BitmapTestActivity.class));}public void <span style="color:#ff0000;">testMatrix</span>(View v) {startActivity(new Intent(this, MatrixTestActivity.class));}public void <span style="color:#ff0000;">clickIV</span>(View v) {Toast.makeText(this, "点击了selector", 0).show();}
}

3、将上图分开来看(从上到下依次展示布局文件或者代码)

3-1、activity_bitmap.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="保存图片" android:onClick="saveImage"/><ImageViewandroid:id="@+id/iv_bitmap1"android:layout_width="fill_parent"android:layout_height="wrap_content" /><ImageViewandroid:id="@+id/iv_bitmap2"android:layout_width="fill_parent"android:layout_height="wrap_content" /><ImageViewandroid:id="@+id/iv_bitmap3"android:layout_width="fill_parent"android:layout_height="wrap_content" />
</LinearLayout>

BitmapTestActivity.java

package com.atguigu.l11_graphics;import java.io.FileNotFoundException;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;/*Bitmap: 加载一张图片数据到内存中, 都可以封装成一个Bitmap对象需求3: 将一个bitmap对象保存到存储空间中*/
public class BitmapTestActivity extends Activity {private ImageView iv_bitmap1;private ImageView iv_bitmap2;private ImageView iv_bitmap3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bitmap);iv_bitmap1 = (ImageView) findViewById(R.id.iv_bitmap1);iv_bitmap2 = (ImageView) findViewById(R.id.iv_bitmap2);iv_bitmap3 = (ImageView) findViewById(R.id.iv_bitmap3);//1: 加载资源文件中的图片资源并显示iv_bitmap1.setImageResource(R.drawable.ic_launcher);//2: 使用bitmapfactory做--加载资源图片Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);iv_bitmap2.setImageBitmap(bitmap);//加载存储空间的图片Bitmap bitmap2 = BitmapFactory.decodeFile("/storage/sdcard/atguigu.png");iv_bitmap3.setImageBitmap(bitmap2);}/*** 讲bitmap对象保存到存储空间去*	/data/data/包名/files/save.png*/public void saveImage(View v) {Bitmap bitmap = BitmapFactory.decodeFile("/storage/sdcard/atguigu.png");try {bitmap.compress(CompressFormat.PNG, 100,openFileOutput("save.png", Context.MODE_PRIVATE));} catch (FileNotFoundException e) {e.printStackTrace();}}
}

3-2、activity_matrix.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="50dip"android:orientation="horizontal" ><EditTextandroid:id="@+id/et_matrix_scale"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:text="0.25" /><EditTextandroid:id="@+id/et_matrix_rotate"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:text="30" /><EditTextandroid:id="@+id/et_matrix_translateX"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:text="10" /><EditTextandroid:id="@+id/et_matrix_translateY"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:text="10" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="50dip"android:orientation="horizontal" ><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:onClick="scaleBitmap"android:text="缩放" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:onClick="rotateBitmap"android:text="旋转" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:onClick="translateBitmap"android:text="移动" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1.0"android:onClick="clearMatrix"android:text="还原" /></LinearLayout><ImageViewandroid:id="@+id/iv_matrix_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" android:scaleType="matrix"/></LinearLayout>

MatrixTestActivity.java

package com.atguigu.l11_graphics;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
/*Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放、平移、旋转等操作*/
public class MatrixTestActivity extends Activity {private EditText et_matrix_scale;private EditText et_matrix_rotate;private EditText et_matrix_translateX;private EditText et_matrix_translateY;private ImageView iv_matrix_icon;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_matrix);et_matrix_scale = (EditText) findViewById(R.id.et_matrix_scale);et_matrix_rotate = (EditText) findViewById(R.id.et_matrix_rotate);et_matrix_translateX = (EditText) findViewById(R.id.et_matrix_translateX);et_matrix_translateY = (EditText) findViewById(R.id.et_matrix_translateY);iv_matrix_icon = (ImageView) findViewById(R.id.iv_matrix_icon);}/*** 缩放图片*/Matrix matrix = new Matrix();public void scaleBitmap(View view) {// 得到缩放比例--float类型float sacle = Float.parseFloat(et_matrix_scale.getText().toString());// 对缩放图片对象设置xy轴缩放比例matrix.postScale(sacle, sacle);iv_matrix_icon.setImageMatrix(matrix);}/*** 旋转图片*/public void rotateBitmap(View view) {float degrees = Float.parseFloat(et_matrix_rotate.getText().toString());matrix.postRotate(degrees);iv_matrix_icon.setImageMatrix(matrix);}/*** 移动图片*/public void translateBitmap(View view) {float dx = Float.parseFloat(et_matrix_translateX.getText().toString());float dy = Float.parseFloat(et_matrix_translateY.getText().toString());matrix.postTranslate(dx, dy);iv_matrix_icon.setImageMatrix(matrix);}/*** 还原操作*/public void clearMatrix(View view) {//清除数据matrix.reset();iv_matrix_icon.setImageMatrix(matrix);}
}

MatrixTestActivity.java

package com.atguigu.l11_graphics;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
/*Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放、平移、旋转等操作*/
public class MatrixTestActivity extends Activity {private EditText et_matrix_scale;private EditText et_matrix_rotate;private EditText et_matrix_translateX;private EditText et_matrix_translateY;private ImageView iv_matrix_icon;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_matrix);et_matrix_scale = (EditText) findViewById(R.id.et_matrix_scale);et_matrix_rotate = (EditText) findViewById(R.id.et_matrix_rotate);et_matrix_translateX = (EditText) findViewById(R.id.et_matrix_translateX);et_matrix_translateY = (EditText) findViewById(R.id.et_matrix_translateY);iv_matrix_icon = (ImageView) findViewById(R.id.iv_matrix_icon);}/*** 缩放图片*/Matrix matrix = new Matrix();public void scaleBitmap(View view) {// 得到缩放比例--float类型float sacle = Float.parseFloat(et_matrix_scale.getText().toString());// 对缩放图片对象设置xy轴缩放比例matrix.postScale(sacle, sacle);iv_matrix_icon.setImageMatrix(matrix);}/*** 旋转图片*/public void rotateBitmap(View view) {float degrees = Float.parseFloat(et_matrix_rotate.getText().toString());matrix.postRotate(degrees);iv_matrix_icon.setImageMatrix(matrix);}/*** 移动图片*/public void translateBitmap(View view) {float dx = Float.parseFloat(et_matrix_translateX.getText().toString());float dy = Float.parseFloat(et_matrix_translateY.getText().toString());matrix.postTranslate(dx, dy);iv_matrix_icon.setImageMatrix(matrix);}/*** 还原操作*/public void clearMatrix(View view) {//清除数据matrix.reset();iv_matrix_icon.setImageMatrix(matrix);}
}
3-3、

shape_test.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 半径大小 --><corners android:radius="10dp" /><!-- 边框 --><strokeandroid:dashGap="2dp"android:dashWidth="2dp"android:width="3dp"android:color="#FF7F00" /><sizeandroid:height="50dp"android:width="40dp" /><!-- 颜色 --><solid android:color="#FFD700"></solid><!-- 覆盖solid --><gradientandroid:startColor="#ffffff"android:centerColor="#EE4000"android:endColor="#ffffff"android:angle="90"/>
</shape>

3-4、


image_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 特别的状态放在前面 --><item android:drawable="@drawable/main_index_search_pressed"  android:state_pressed="true"/><item android:drawable="@drawable/main_index_search_normal"/>
</selector>

3-5、


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:state_pressed="true"><shape><corners android:radius="4dp"></corners><stroke android:width="2dp" android:color="#EEAD0E" android:dashWidth="4dp" android:dashGap="2dp"></stroke><size android:height="40dp"></size><gradient android:startColor="#ffffff" android:centerColor="#ffffff" android:endColor="#E0FFFF"/></shape></item><item><shape><corners android:radius="2dp"></corners><stroke android:width="2dp" android:color="#EE7AE9"></stroke><size android:height="40dp"></size><solid android:color="#E0FFFF"></solid></shape></item></selector>

3-6、(9patch图片)


4、startActivity(new Intent(this, DrawTestActivity.class));启动下面图片


DrawTestActivity.java

package com.atguigu.l11_graphics;import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
public class DrawTestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 显示自定义视图setContentView(new MyView(this));}/*** 自定义myview视图*/private class MyView extends View {//成员变量---可画的图形对象private ShapeDrawable shapeDrawable;public MyView(Context context) {super(context);// 初始化一个图形对象---参数是椭圆shapeDrawable = new ShapeDrawable(new OvalShape());// 通过椭圆得到画笔,通过画笔设置颜色shapeDrawable.getPaint().setColor(Color.RED);// 指定位置left top right bottomshapeDrawable.setBounds(10, 10, 200, 100);}// 显示界面视图效果 画布@Overrideprotected void onDraw(Canvas canvas) {//设置画布的颜色canvas.drawColor(Color.GREEN);// 将图像画到画布上shapeDrawable.draw(canvas);//指定画笔Paint paint = new Paint();//通过画笔设置颜色paint.setColor(Color.BLUE);//设置字体大小paint.setTextSize(30);//设置平滑度paint.setAntiAlias(true);//在画布上写上字体canvas.drawText("你好", 10, 200, paint);}}
}




这篇关于Graphics简单汇总的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125364

相关文章

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文