hallo,又是我鑫鑫,这次博客给大家带来纯色图制作APP代码,
在这之前要感谢之两位大佬的代码,这个程序是我通过结合两者代码,改编出来的。
在这里感谢这两位大佬
那么感谢也感谢了,直接上代码吧
MainActivity.java
package com.mycompany.myapp;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.net.*;
import android.os.*;
import android.provider.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.SeekBar.*;
import java.io.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private SeekBar red_sb,green_sb,blue_sb;
private ImageView imageView;
private Canvas canvas;
private Paint paint;
private Bitmap baseBitmap,copyBitmap;
private float red_vector,green_vector,blue_vector;
Bitmap cacheBitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
red_sb=(SeekBar) findViewById(R.id.red_seekbar);
green_sb=(SeekBar) findViewById(R.id.green_seekbar);
blue_sb=(SeekBar) findViewById(R.id.blue_seekbar);
imageView=(ImageView) findViewById(R.id.im);
red_sb.setOnSeekBarChangeListener(this);
green_sb.setOnSeekBarChangeListener(this);
blue_sb.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int progress=seekBar.getProgress();
float count=progress/50f;//使拖动条的取值为0f-2f,满足我们的取值要求
switch (seekBar.getId()) {
case R.id.red_seekbar:
this.red_vector=count;
break;
case R.id.green_seekbar:
this.green_vector=count;
break;
case R.id.blue_seekbar:
this.blue_vector=count;
break;
default:
break;
}
//Button SaveInfo =(Button) findViewById(R.id.save);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("信息提示")//设置对话框标题
.setMessage("你确定要保存该图片吗?\n\n保存路径:\nsdcard/鑫鑫工具箱/纯色图制作")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "请调整颜色", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件,点击事件没写,自己添加
// TODO 自动生成的方法存根
imageView.buildDrawingCache(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
saveBitmapFile(bitmap);
imageView.setDrawingCacheEnabled(false);
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
})
.show();//在按键响应事件中显示此对话框
}
});
//主题代码
baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.Standard_background);
copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
canvas=new Canvas(copyBitmap);
Matrix matrix=new Matrix();
paint=new Paint();
//vector:取值范围(0-2)
float[] colors=new float[]{
red_vector,0,0,0,0,
0,green_vector,0,0,0,
0,0,blue_vector,0,0,
0,0,0,1,0};
paint.setColorFilter(new ColorMatrixColorFilter(colors));
canvas.drawBitmap(baseBitmap, matrix, paint);
imageView.setImageBitmap(copyBitmap);
}
public void saveBitmapFile(Bitmap bitmap){
File temp = new File("/sdcard/鑫鑫工具箱/纯色图制作");//要保存文件先创建文件夹
if (!temp.exists()) {
temp.mkdir();
}
//重复保存时,覆盖原同名图片
File file=new File("/sdcard/鑫鑫工具箱/纯色图制作/成品.jpg");//将要保存图片的路径和图片名称
// File file = new File("/sdcard/1delete/1.png");/延时较长
try {
BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*public void deleteFile(View v){//点击按钮删除这个文件
File file = new File("/sdcard/1spray/1.png");
if(file.exists()){
file.delete();
}
}///deleteFile
*/
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#FFFFFFFF">
<LinearLayout
android:layout_height="100dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="纯色图制作"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R"
android:gravity="center"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/red_seekbar"
android:layout_weight="1.0"
android:background="@drawable/Sliding_background"
android:thumb="@drawable/Sliding_button_background"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G"
android:gravity="center"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/green_seekbar"
android:background="@drawable/Sliding_background"
android:thumb="@drawable/Sliding_button_background"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:gravity="center"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blue_seekbar"
android:background="@drawable/Sliding_background"
android:thumb="@drawable/Sliding_button_background"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/im"
android:layout_weight="1.0"
android:src="@drawable/Standard_background"/>
</LinearLayout>
对应资源文件
Sliding_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="10dp" android:height="10dp">
<!--图形形状-->
<shape android:shape="oval">
<!--图形内圈颜色-->
<solid android:color="#20a4f9" />
<!--图形大小设置-->
<size android:height="13dp" android:width="13dp"/>
<!--外环设置-->
<stroke android:width="0dp" android:color="#ffffff"/>
</shape>
</item>
</selector>
Sliding_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dp"/>
<solid android:color="#ffffff"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dp"/>
<solid android:color="#20a4f9"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dp"/>
<solid android:color="#20a4f9" />
</shape>
</clip>
</item>
</layer-list>
对应图片资源:Standard_background.png
由于图片是白色的,有可能会背景色重叠,但不影响使用。
当然最后不要忘记添加保存权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
整篇文章到这就结束了,感谢各位的观看。
如果程序有什么bug还行,反馈给我。
留下邮箱,直接给源码,或者克隆源码
各位再见。 —by 鑫鑫