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">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="testTuPian"
- android:text="測试图片处理" />
- <Button
- android: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 {
- @Override
- protected 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));启动的界面
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
上图布局文件例如以下
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">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick=<span style="color:#ff0000;">"testBD"</span>
- android:text="測试Bitmap" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="<span style="color:#ff0000;">testMatrix</span>"
- android:text="測试图片的缩放等处理" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="使用Shape做的button"
- 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>"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="使用Selector+Shape做的button"
- android:background="@drawable/shape_selector"/>
- <TextView
- android: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 {
- @Override
- protected 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" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="保存图片"
- android:onClick="saveImage"/>
- <ImageView
- android:id="@+id/iv_bitmap1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <ImageView
- android:id="@+id/iv_bitmap2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <ImageView
- android: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;
- @Override
- protected 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
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
- <?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" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/et_matrix_scale"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="0.25" />
- <EditText
- android:id="@+id/et_matrix_rotate"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="30" />
- <EditText
- android:id="@+id/et_matrix_translateX"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="10" />
- <EditText
- android:id="@+id/et_matrix_translateY"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:text="10" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:orientation="horizontal" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:onClick="scaleBitmap"
- android:text="缩放" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:onClick="rotateBitmap"
- android:text="旋转" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:onClick="translateBitmap"
- android:text="移动" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:onClick="clearMatrix"
- android:text="还原" />
- </LinearLayout>
- <ImageView
- android: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;
- @Override
- protected 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;
- @Override
- protected 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" />
- <!-- 边框 -->
- <stroke
- android:dashGap="2dp"
- android:dashWidth="2dp"
- android:width="3dp"
- android:color="#FF7F00" />
- <size
- android:height="50dp"
- android:width="40dp" />
- <!-- 颜色 -->
- <solid android:color="#FFD700"></solid>
- <!-- 覆盖solid -->
- <gradient
- android:startColor="#ffffff"
- android:centerColor="#EE4000"
- android:endColor="#ffffff"
- android:angle="90"/>
- </shape>
3-4、
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
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、
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
- <?
- 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 {
- @Override
- protected 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 bottom
- shapeDrawable.setBounds(10, 10, 200, 100);
- }
- // 显示界面视图效果 画布
- @Override
- protected 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简单汇总的更多相关文章
- Python中对时间日期的处理方法简单汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- Linux命令面试常考的简单汇总
1.显示日期与时间的命令:date 2.显示日历的命令:cal 3.简单好用的计算器:bc 4.热键“命令补全或文件补齐”:Tab 5.热键“中断目前程序”:Ctrl+C 6.热键“键盘输入结束(En ...
- Linux内存简单汇总
Linux内存主要用来存储系统和应用程序的指令,数据,缓存等 一,内存映射 1,内核给每个进程提供一个独立的虚拟机地址空间,并且这个地址空间是连续的 2,虚拟地址空间内部又被分为内核空间和用户空间 3 ...
- [computer graphics]简单光照模型(Phong和Blinn-Phong)和明暗处理
简单光照模型(Phong和Blinn-Phong)和明暗处理 支持点光源和平行光,是一种简单光照模型,它将光照分解成了三个部分,分别为 漫反射 镜面反射 环境光 如图所示,是一个简单的几何模型. \( ...
- Openstack架构概念图-简单汇总
OpenStack是一个云平台管理的项目,它不是一个软件.这个项目由几个主要的组件组合起来完成一些具体的工作.想要了解openstack,第一步我们可以观察他的概念图: 针对上图的翻译+解释: 上图主 ...
- Spring常用注解简单汇总
使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test&qu ...
- sonar阻断级别错误(block)简单汇总
1.代码里面包含PASSWORD.PWD 'PWD' detected in this expression, review this potentially hardcoded credential ...
- HTML5 之 简单汇总
参考: HTML5的十大新特性 前端面试必备之html5的新特性 HTML5 1.语义化元素 1.1结构元素 标签 描述 article 表示与上下文不相关的独立内容区域 aside 定义页面的侧边栏 ...
- 【转】iOS 7免费设计资源汇总
原文链接:http://mobile.51cto.com/hot-406317.htm#585532-tsina-1-28470-7e393678b940a4d55500bf3feae3d2e9 以下 ...
随机推荐
- 【Codeforces1109B_CF1109B】Sasha and One More Name(字符串)
题目: Codeforces1109B 我打的是 Div2 ,所以我看到的题号实际上是 1113D -- 考场上傻了没敢大力猜结论没做出来这道题,不幸掉分-- 1869->1849 嘤嘤嘤 翻译 ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 C-Cities
题目描述:There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirec ...
- ANDROID 开发之 SQLite
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 阿里云虚拟主机针对恶意频繁攻击式访问造成CPU爆满的解决方法
最近网站CPU经常爆满,到阿里云提交了工单,工程师给我的处理意见: 您好,虚拟主机CPU占用比较高通常这种情况有两种可能: 一是网站应用程序代码逻辑较复杂,或业务架构效率比较低,在请求了某个网 ...
- SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集
SAS进阶<深入解析SAS>之Base SAS基础.读取外部数据到SAS数据集 前言:在学习完<SAS编程与商业案例>后,虽然能够接手公司的基本工作,但是为了更深入的SAS学习 ...
- Android 根据QQ号跳转到QQ聊天界面
从自己开发的应用中根据QQ号跳转到QQ应用的聊天界面,实现起来很方便: 即: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(" ...
- Element type "LinearLayout" must be followed by either attribute specifications, ">" or "/>"的解决办法
看老师的word文档开始学习.复制了一段代码,在layout中新建了一个Android XML file,发现有提示错误. 代码如下: <?xml version="1.0" ...
- 7、scala面向对象编程之类
1. 定义一个简单的类 2.getter与setter 3.自定义getter与setter方法 4.仅暴露field的getter方法 5.private[this]的使用 6.Java风格的ge ...
- 【VHDL】组合逻辑电路和时序逻辑电路的区别
简单的说,组合电路,没有时钟:时序电路,有时钟. ↓ 也就是说,组合逻辑电路没有记忆功能,而时序电路具有记忆功能. ↓ 在VHDL语言中,不完整条件语句对他们二者的影响分别是什么?组合逻辑中可能生成锁 ...