非常好看的android音量旋钮

本文实例为大家分享了好看的android音量旋钮,供大家参考,具体内容如下

效果图:

实现思路,用的自定义的控件,图片和按钮都是自己绘制的,并且附带点击事件,可以监听当前的旋钮的值:

第一步:先把布局写了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="#000000"
 android:layout_width="match_parent"
 android:gravity="center"
 android:layout_height="match_parent">
 <TextView
  android:id="@+id/tv"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="#ffffff"
  />
 <LinearLayout
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:layout_marginBottom="10dp"
  android:orientation="horizontal">

  <com.example.longshine.zname.AnalogController
   android:id="@+id/controllerBass"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:background="#000000" />

 </LinearLayout>

</LinearLayout>

第二步:然后把自定义的控件类写了:AnalogController

 import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.util.AttributeSet;
  import android.view.MotionEvent;
  import android.view.View;

/**
 * Created by Harjot on 23-May-16.
 */
public class AnalogController extends View {

 static float width, height;
 float midx, midy;
 Paint textPaint;
 Paint circlePaint;
 public Paint circlePaint2;
 public Paint linePaint;
 String angle;
 float currdeg, deg = 3, downdeg, prevCurrDeg;
 boolean isIncreasing, isDecreasing;
 public static int themeColor = Color.parseColor("#B24242");

 int progressColor, lineColor;

 onProgressChangedListener mListener;

 String label;

 public interface onProgressChangedListener {
  void onProgressChanged(int progress);
 }

 public void setOnProgressChangedListener(onProgressChangedListener listener) {
  mListener = listener;
 }

 public AnalogController(Context context) {
  super(context);
  init();
 }

 public AnalogController(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public AnalogController(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }

 void init() {
  textPaint = new Paint();
  textPaint.setColor(Color.WHITE);
  textPaint.setStyle(Paint.Style.FILL);
  textPaint.setTextSize(20);
  textPaint.setFakeBoldText(true);
  textPaint.setTextAlign(Paint.Align.CENTER);
  circlePaint = new Paint();
  circlePaint.setColor(Color.parseColor("#222222"));
  circlePaint.setStyle(Paint.Style.FILL);
  circlePaint2 = new Paint();
  circlePaint2.setColor(themeColor);
//  circlePaint2.setColor(Color.parseColor("#FFA036"));
  circlePaint2.setStyle(Paint.Style.FILL);
  linePaint = new Paint();
  linePaint.setColor(themeColor);
//  linePaint.setColor(Color.parseColor("#FFA036"));
  linePaint.setStrokeWidth(7);
  angle = "0.0";
  label = "Label";
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  midx = canvas.getWidth() / 2;
  midy = canvas.getHeight() / 2;

  int ang = 0;
  float x = 0, y = 0;
  int radius = (int) (Math.min(midx, midy) * ((float) 14.5 / 16));
  float deg2 = Math.max(3, deg);
  float deg3 = Math.min(deg, 21);
  for (int i = (int) (deg2); i < 22; i++) {
   float tmp = (float) i / 24;
   x = midx + (float) (radius * Math.sin(2 * Math.PI * (1.0 - tmp)));
   y = midy + (float) (radius * Math.cos(2 * Math.PI * (1.0 - tmp)));
   circlePaint.setColor(Color.parseColor("#111111"));
   canvas.drawCircle(x, y, ((float) radius / 15), circlePaint);
  }
  for (int i = 3; i <= deg3; i++) {
   float tmp = (float) i / 24;
   x = midx + (float) (radius * Math.sin(2 * Math.PI * (1.0 - tmp)));
   y = midy + (float) (radius * Math.cos(2 * Math.PI * (1.0 - tmp)));
   canvas.drawCircle(x, y, ((float) radius / 15), circlePaint2);
  }

  float tmp2 = (float) deg / 24;
  float x1 = midx + (float) (radius * ((float) 2 / 5) * Math.sin(2 * Math.PI * (1.0 - tmp2)));
  float y1 = midy + (float) (radius * ((float) 2 / 5) * Math.cos(2 * Math.PI * (1.0 - tmp2)));
  float x2 = midx + (float) (radius * ((float) 3 / 5) * Math.sin(2 * Math.PI * (1.0 - tmp2)));
  float y2 = midy + (float) (radius * ((float) 3 / 5) * Math.cos(2 * Math.PI * (1.0 - tmp2)));

  circlePaint.setColor(Color.parseColor("#222222"));
  canvas.drawCircle(midx, midy, radius * ((float) 13 / 15), circlePaint);
  circlePaint.setColor(Color.parseColor("#000000"));
  canvas.drawCircle(midx, midy, radius * ((float) 11 / 15), circlePaint);
  canvas.drawText(label, midx, midy + (float) (radius * 1.1), textPaint);
  canvas.drawLine(x1, y1, x2, y2, linePaint);

 }

 @Override
 public boolean onTouchEvent(MotionEvent e) {

  mListener.onProgressChanged((int) (deg - 2));

  if (e.getAction() == MotionEvent.ACTION_DOWN) {
   float dx = e.getX() - midx;
   float dy = e.getY() - midy;
   downdeg = (float) ((Math.atan2(dy, dx) * 180) / Math.PI);
   downdeg -= 90;
   if (downdeg < 0) {
    downdeg += 360;
   }
   downdeg = (float) Math.floor(downdeg / 15);
   return true;
  }
  if (e.getAction() == MotionEvent.ACTION_MOVE) {
   float dx = e.getX() - midx;
   float dy = e.getY() - midy;
   currdeg = (float) ((Math.atan2(dy, dx) * 180) / Math.PI);
   currdeg -= 90;
   if (currdeg < 0) {
    currdeg += 360;
   }
   currdeg = (float) Math.floor(currdeg / 15);

   if (currdeg == 0 && downdeg == 23) {
    deg++;
    if (deg > 21) {
     deg = 21;
    }
    downdeg = currdeg;
   } else if (currdeg == 23 && downdeg == 0) {
    deg--;
    if (deg < 3) {
     deg = 3;
    }
    downdeg = currdeg;
   } else {
    deg += (currdeg - downdeg);
    if (deg > 21) {
     deg = 21;
    }
    if (deg < 3) {
     deg = 3;
    }
    downdeg = currdeg;
   }

   angle = String.valueOf(String.valueOf(deg));
   invalidate();
   return true;
  }
  if (e.getAction() == MotionEvent.ACTION_UP) {
   return true;
  }
  return super.onTouchEvent(e);
 }

 public int getProgress() {
  return (int) (deg - 2);
 }

 public void setProgress(int x) {
  deg = x + 2;
 }

 public String getLabel() {
  return label;
 }

 public void setLabel(String txt) {
  label = txt;
 }

 public int getLineColor() {
  return lineColor;
 }

 public void setLineColor(int lineColor) {
  this.lineColor = lineColor;
 }

 public int getProgressColor() {
  return progressColor;
 }

 public void setProgressColor(int progressColor) {
  this.progressColor = progressColor;
 }
}

第三步:在MainActivity中,我们去写监听方法,查看旋钮的值:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by 16857 on 2019/4/12.
 */

public class TextActivity extends Activity {
 AnalogController bassController;
 public static int themeColor = Color.parseColor("#B24242");
 private TextView tv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  bassController = (AnalogController) findViewById(R.id.controllerBass);
  tv = (TextView)findViewById(R.id.tv);

  bassController.setLabel("BASS");

  bassController.circlePaint2.setColor(themeColor);
  bassController.linePaint.setColor(themeColor);
  bassController.invalidate();
  bassController.linePaint.setColor(themeColor);

  bassController.setOnProgressChangedListener(new AnalogController.onProgressChangedListener() {
   @Override
   public void onProgressChanged(int progress) {
    tv.setText(progress+"");
   }
  });

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android Studio中Run按钮是灰色的快速解决方法

    首先是,在不同的AS中,gradle版本不同,下载的sdk版本不同,这些,都在gradle(Project.Models)相关代码里调过来就好.之前的文章里有说过. 经过调好gradle这些文件,AS已经可以built 成功后. 下一步,Run the application. 这时候,遇到问题:Run按钮灰色,失效. 点击Run旁边 Select Run/Debug Configuration按钮 选择 Edit Configuration,于是: 在model下拉框中选择app.如果下拉框中

  • Android按钮单击事件的四种常用写法总结

    很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可以有一定的参考借鉴价值. xml文件代码如下: <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_conte

  • 基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出

    Android Material Design:PopupMenu Android Material Design 引入的PopupMenu类似过去的上下文菜单,但是更灵活. 如图所示: 现在给出实现上图PopupMenu的代码. 本例是一个普通的Button触发弹出PopupMenu. 测试的MainActivity.java : package zhangphil.materialdesign; import android.app.Activity; import android.os.B

  • Android开发悬浮按钮 Floating ActionButton的实现方法

    一.介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 android.support.design.widget.FloatingActionButton 二.使用准备, 在as 的 build.grade文件中写上 compile 'com.android.support:design:22.2.0' 三.使用说明 xml文件中,注意蓝色字体部分 <android.support.design.widget.FloatingActionButt

  • Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法

    背景:在写登录界面时,老板就觉得在输入密码的时候谈出来的输入法软键盘把登录按钮遮挡住了(入下图所示,不爽),连输入框都被挡了一半,于是不满意了,要叫我改,于是我看QQ的登录效果,我就去研究了一下,弹出输入法整个布局上来了,终于让老板满意了. (如上图这样,老板不满意的,呵呵) 1,咱们就解决问题吧. 我看了很多博客和问答,很多人都说直接在在AndroidManifest.xml中给这个Activity设置 <activity android:windowSoftInputMode="sta

  • Android中让按钮拥有返回键功能的方法及重写返回键功能

    让按钮拥有返回键的功能很简单,在点击事件加上finish();就OK了. 如: 复制代码 代码如下: public void onClick(View v){ finish(); } finish() 仅仅是把activity从当前的状态退出,但是资源并没有给清理. 其实android的机制决定了用户无法完全退出application,即使用System.exit(). android自己决定何时该从内存中释放程序,当系统没有可用内存时,就会按照一定的优先级来销毁应用程序. android手机操

  • Android仿知乎悬浮功能按钮FloatingActionButton效果

    前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,所以就来实践实践.效果基本出来了,大家可以自己去完善. 首先看一下效果图: 我们看到点击FloatingActionButton后会展开一些item,然后会有一个蒙板效果,这都是这个View的功能.那么这整个View肯定是个ViewGroup,我们一部分一部分来看. 首先是这个最小的Tag: 这个Tag带文字,可以是一个TextView,但为了美观,我们使用CardView,CardView是一个FrameLayout,我们要让它具有显

  • Android实现点击AlertDialog上按钮时不关闭对话框的方法

    本文实例讲述了Android实现点击AlertDialog上按钮时不关闭对话框的方法.分享给大家供大家参考.具体如下: 开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和build

  • android为ListView每个Item上面的按钮添加事件

    本文介绍了ListView给每个Item上面的按钮添加事件,具体如下: 1.先看下效果图: 在这里仅供测试,我把数据都写死了,根据需要可以自己进行修改,此外实现ListView上面每个Item上的Button的事件有两种方法: 1.使用final变量扩展局部变量的生命周期范围主要代码(本文最后附全部代码): //注意原本getView方法中的int position变量是非final的,现在改为final @Override public View getView(final int posit

  • Android按钮按下的时候改变颜色实现方法

    需求是在我按下按钮时,该变按钮颜色,使用户感觉到自己按了按钮,当松开的时候,变回原来的颜色. 正常时: 按下时: 有人说,直接监听按钮的按下事件不得了嘛,其实这样确实能实现同样的效果,但是有个缺点,比如很多按钮都需要这样的效果,那你同样的代码就要重复很多次.所以,还是要通用起来. 首先,在res文件夹下新建一个文件夹drawable,这是无关分辨率的: 在下面建立一个xml文件:login_button_selector.xml 复制代码 代码如下: <selector xmlns:androi

随机推荐