AndroidStdio 程序目录介绍:
![](https://img.haomeiwen.com/i18961949/141e34d3360241cc.png)
注意:
Android里面的大部分控件都会提供很多方法,这些方法只需要自己去重写,不需要自己主动去调用
Activity的生命周期:
![](https://img.haomeiwen.com/i18961949/f4e23f815e4bb917.png)
@Override //界面启动了 展现出来了
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
@Override // 重新启动一个界面
protected void onRestart() {
super.onRestart();
System.out.println("onRestart");
}
@Override//恢复界面 后台->前台
protected void onResume() {
super.onResume();
System.out.println("onResume");
}
@Override//暂停界面 界面切换
protected void onPause() {
super.onPause();
System.out.println("onPause");
}
@Override //销毁界面
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
}
程序一开始就会创建一个任务,任务用来存放当前的活动,任务通常是一个栈结构,常称作栈结构
![](https://img.haomeiwen.com/i18961949/974dd7f339fb9263.png)
任务:
![](https://img.haomeiwen.com/i18961949/90d02045228cfde3.png)
任务栈:
位于栈顶的活动:ForegroundActivity
![](https://img.haomeiwen.com/i18961949/5878ba302db7dea9.png)
栈的基本行为:
![](https://img.haomeiwen.com/i18961949/9ff9808237e3defb.png)
活动的状态:
![](https://img.haomeiwen.com/i18961949/1506cb00b0a43681.png)
父视图、 子视图:‘
- 将一个控件添加到一个容器中 控件就是这个容器的子视图 容器就是这个控件的父视图*(一个控件就是一个类的一个具体对象)
- 1.match_parent 和父视图一样大
- 2.wrap_content 包裹内容 和控件的内容一样大
- 3.20dp 具体尺寸
使用java代码来布局界面
- 通过添加id号可以唯一标识某一个控件 或者组件(容器)
BitMap
常用的静态方法:
*public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像
*public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
int dstHeight, boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变。
*public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图
*public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。
*public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
BitmapFactory工厂类:
读取一个资源文件得到一个位图。如果位图数据不能被解码,或者opts参数只请求大小信息时,则返回NuLL。
(即当Options.inJustDecodeBounds=true,只请求图片的大小信息。)
public static Bitmap decodeResource(Resources res, int id)
public static Bitmap decodeResource(Resources res, int id, Options opts)
BitmapDrawable类:
继承于Drawable,你可以从文件路径、输入流、XML文件以及Bitmap中创建。
常用的构造函数:
Resources res=getResources();//获取资源
public BitmapDrawable(Resources res)——创建一个空的drawable。(Response用来指定初始时所用的像素密度)替代public BitmapDrawable()方法(此方法不处理像素密度)
public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap
public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap.
public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream.
image.png
撕衣服Demo
首先添加撕衣服前后的两张图片
![](https://img.haomeiwen.com/i18961949/ec09ec692e21e772.png)
**
* 撕衣服的思路
* 使用透明色去替换原有图片的对应点的像素
* 立刻获取替换之后的图片 将图片显示在ImageView上
*/
public class MainActivity extends AppCompatActivity {
ImageView forground;
Bitmap copyBitmap; //复制后的图片
Paint paint; //画笔
Bitmap orgBitmap;//原始图片
Canvas canvas;//画布
@Override//创建一个界面 界面如何布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 配置界面
setContentView(R.layout.activity_main);
![](https://img.haomeiwen.com/i18961949/75e1a56281d2319a.png)
//找到容器里面的图片视图控件
//findViewById
forground = findViewById(R.id.iv_forground);
//将需要操作的图片读取出来 Bitmap
//BItmapFactory :用于管理位图图片
//decodeResoures 从工程的资源路径中去查找一张位图
//getresources 获取工程的资源
//R.drawble.fr 访问资源路径下 drawable里面的一个文件fr的资源
orgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.fr);
//操作这张图片 用透明色去替换某个位置的颜色
//不能操作原图 只能拷贝一份
// 创建一个和原始图片相同的空位图
copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(),orgBitmap.getHeight(),orgBitmap.getConfig());
//创建一个Canvas 画布-现实中的画板
canvas = new Canvas(copyBitmap);
//创建一个画笔
paint = new Paint();
//创建一个矩阵
Matrix matrix = new Matrix();
//旋转图片
// matrix.setRotate(90);
//平移图片
// matrix.setTranslate(300.0f,0f);
//翻转 set只作用一次,post作用多次
// matrix.setScale(-1f,1f);
// matrix.postTranslate(orgBitmap.getWidth(),0);
//画一副图
canvas.drawBitmap(orgBitmap,matrix,paint );
//显示图片
forground.setImageBitmap(copyBitmap);
//给前景图片添加touch事件
//当有触摸事件发生 系统就会将这个事件接收并回调这个事件
forground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
//获取当前事件
int action = event.getAction();
//判断状态
if(action==MotionEvent.ACTION_MOVE){
//获得触摸点坐标
int x= (int) event.getX();
int y= (int) event.getY();
//替换xy对应的像素
for(int i = -8;i<8;i++){
for(int j = -8;j<8;j++){
copyBitmap.setPixel(x+i,y+j,Color.TRANSPARENT);
}
}
//canvas.drawBitmap(orgBitmap,new Matrix(),paint);
forground.setImageBitmap(copyBitmap);
}
return true;
}
});
}
public void code(){
//通过代码来布局界面
//1.找一个容器 xxlayout
FrameLayout container = new FrameLayout(this);
//设置当前这个界面的内容视图为这个容器
//setContentView(container);
//3.创建一个子视图
ImageView bgImageView = new ImageView(this);
//设置属性
bgImageView.setBackgroundColor(Color.GREEN);
//添加到容器里面
container.addView(bgImageView,200,200);
setContentView(container);
}
XML文件:
![](https://img.haomeiwen.com/i18961949/c261d8e739c14ace.png)
![](https://img.haomeiwen.com/i18961949/4711c2899c006c9a.png)
![](https://img.haomeiwen.com/i18961949/c7759c7150b69838.png)
运行结果:
image.png
![](https://img.haomeiwen.com/i18961949/f29563ee1700ddb3.png)
网友评论