姓氏制作成头像packagecom atomic moretool importandroi graphics Bitmap importandroi graphics Canvas importandroi graphics Color importandroi graphics Paint importandroi graphics drawable BitmapDrawab importandroi os Bundle importandroi os Environ 制作头像的代码
package com.atomic.moretool; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileOutputStream; import java.util.Random; public class NameToHeader extends AppCompatActivity {
private TextView saveBgColor,saveTxtColor; private EditText nametoHeader,nameH,picW,picH; private ImageView showHeader; private final String[] strs={
"红色","黄色","蓝色","黑色","绿色","灰色","白色"}; private final String[] strings={
"#ff0000","#ffff00","#00ffff","#000000","#00ff00","#d9d9d9","#ffffff"}; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_name_to_header); init(); } private void init() {
saveBgColor=findViewById(R.id.save_bg_color); saveTxtColor=findViewById(R.id.save_txt_color); nametoHeader=findViewById(R.id.name_to_header); Button nametoBtn = findViewById(R.id.name_to_btn); Button saveName=findViewById(R.id.save_name); showHeader=findViewById(R.id.show_header); nameH=findViewById(R.id.name_h); picW=findViewById(R.id.pic_w); picH=findViewById(R.id.pic_h); nametoHeader.setSelectAllOnFocus(true); nameH.setSelectAllOnFocus(true); picW.setSelectAllOnFocus(true); picH.setSelectAllOnFocus(true); ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.spinner_item_select,strs); adapter.setDropDownViewResource(R.layout.spinner_item_dropdown); Spinner chooseColor = findViewById(R.id.spinner); chooseColor.setPrompt("选择背景颜色"); chooseColor.setAdapter(adapter); chooseColor.setSelection(0); chooseColor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
saveBgColor.setText(strings[i]); } @Override public void onNothingSelected(AdapterView<?> adapterView) {
} }); Spinner choosetxtc = findViewById(R.id.spinner_text); choosetxtc.setAdapter(adapter); choosetxtc.setSelection(6); chooseColor.setPrompt("选择文字颜色"); choosetxtc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
saveTxtColor.setText(strings[i]); } @Override public void onNothingSelected(AdapterView<?> adapterView) {
} }); nametoBtn.setOnClickListener(view->{
String name=nametoHeader.getText().toString().trim(); String nameh=nameH.getText().toString().trim(); String picw=picW.getText().toString().trim(); String pich=picH.getText().toString().trim(); String bgColor=saveBgColor.getText().toString().trim(); String txtColor=saveTxtColor.getText().toString().trim(); if (!name.equals("") && !nameh.equals("") && !picw.equals("") && !pich.equals("") &&!bgColor.equals("") && !txtColor.equals("")){
Log.i("tag","选择了背景颜色"+bgColor); Log.i("tag","选择了文字颜色"+txtColor); Bitmap bg=Bitmap.createBitmap(Integer.parseInt(picw),Integer.parseInt(pich),Bitmap.Config.ARGB_8888); Canvas bgCanvas=new Canvas(bg); bgCanvas.drawColor(Color.parseColor(bgColor)); try {
Bitmap txt=stringtoBitmap(Float.parseFloat(nameh),name,txtColor); int left=(Integer.parseInt(picw)-Integer.parseInt(nameh))/2; int top=(Integer.parseInt(pich)-Integer.parseInt(nameh))/2-Integer.parseInt(nameh)/10; bgCanvas.drawBitmap(txt,left,top,null); showHeader.setImageBitmap(bg); }catch (Exception e){
e.printStackTrace(); } }else{
Toast.makeText(this, "输入宽高与姓氏,选择背景与文字颜色", Toast.LENGTH_SHORT).show(); } }); saveName.setOnClickListener(view -> {
try {
if (showHeader.getDrawable() !=null){
Bitmap bm =((BitmapDrawable) showHeader.getDrawable()).getBitmap(); String savepath = Environment.getExternalStorageDirectory().getAbsolutePath().trim() + "/Atomic/" +nametoHeader.getText().toString().trim()+new Random().nextInt() +".jpg"; File file = new File(savepath); if (!file.exists()) {
file.getParentFile().mkdirs(); file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); Toast.makeText(this, "保存到Atomic文件夹", Toast.LENGTH_SHORT).show(); }else{
Toast.makeText(this, "先制作", Toast.LENGTH_SHORT).show(); } }catch (Exception e){
e.printStackTrace(); } }); } public static Bitmap stringtoBitmap(float textSize, String text,String txtColor) {
Paint paint = new Paint(); paint.setTextSize(textSize); paint.setTextAlign(Paint.Align.LEFT); paint.setColor(Color.parseColor(txtColor)); Paint.FontMetricsInt fm = paint.getFontMetricsInt(); int width = (int)paint.measureText(text); int height = fm.descent - fm.ascent; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawText(text, 0, fm.leading - fm.ascent, paint); canvas.save(); return bitmap; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_margin="15sp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="姓氏:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:maxLength="1" android:inputType="text" android:layout_marginBottom="10sp" android:textSize="14sp" android:id="@+id/name_to_header" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="文字高:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="200" android:inputType="text" android:layout_marginBottom="10sp" android:textSize="14sp" android:id="@+id/name_h" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="图片宽:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="400" android:inputType="number" android:layout_marginBottom="10sp" android:textSize="14sp" android:id="@+id/pic_w" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="图片高:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:inputType="number" android:text="400" android:layout_marginBottom="10sp" android:textSize="14sp" android:id="@+id/pic_h" android:layout_width="match_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_marginBottom="5sp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="选择背景颜色:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Spinner android:dropDownVerticalOffset="45dp" android:background="@null" android:spinnerMode="dropdown" android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/save_bg_color" android:layout_width="0sp" android:layout_height="0sp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="选择文字颜色:" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Spinner android:dropDownVerticalOffset="45dp" android:background="@null" android:spinnerMode="dropdown" android:id="@+id/spinner_text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/save_txt_color" android:layout_width="0sp" android:layout_height="0sp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="制作" android:textSize="14sp" android:id="@+id/name_to_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="保存" android:textSize="14sp" android:id="@+id/save_name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <ImageView android:id="@+id/show_header" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
今天的文章
姓氏制作成头像分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/96789.html