add paintdrawable,modify shapedrawable

This commit is contained in:
侯歌 2021-12-08 16:44:49 +08:00
parent 2a935f73c0
commit fc5d950d4e
8 changed files with 175 additions and 34 deletions

44
src/gui/core/displaymetrics.cc Executable file
View File

@ -0,0 +1,44 @@
#include <core/displaymetrics.h>
namespace cdroid{
int DisplayMetrics::DENSITY_DEVICE=DENSITY_DEFAULT;
int DisplayMetrics::DENSITY_DEVICEE_STABLE=DENSITY_DEFAULT;
DisplayMetrics::DisplayMetrics(){
}
void DisplayMetrics::setTo(const DisplayMetrics& o) {
if (this == &o)return;
widthPixels = o.widthPixels;
heightPixels= o.heightPixels;
density = o.density;
densityDpi = o.densityDpi;
scaledDensity = o.scaledDensity;
xdpi = o.xdpi;
ydpi = o.ydpi;
noncompatWidthPixels = o.noncompatWidthPixels;
noncompatHeightPixels= o.noncompatHeightPixels;
noncompatDensity = o.noncompatDensity;
noncompatDensityDpi= o.noncompatDensityDpi;
noncompatScaledDensity = o.noncompatScaledDensity;
noncompatXdpi = o.noncompatXdpi;
noncompatYdpi = o.noncompatYdpi;
}
void DisplayMetrics::setToDefaults() {
widthPixels = 0;
heightPixels = 0;
density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
densityDpi = DENSITY_DEVICE;
scaledDensity = density;
xdpi = DENSITY_DEVICE;
ydpi = DENSITY_DEVICE;
noncompatWidthPixels = widthPixels;
noncompatHeightPixels= heightPixels;
noncompatDensity = density;
noncompatDensityDpi = densityDpi;
noncompatScaledDensity = scaledDensity;
noncompatXdpi = xdpi;
noncompatYdpi = ydpi;
}
}

39
src/gui/core/displaymetrics.h Executable file
View File

@ -0,0 +1,39 @@
#ifndef __DISPLAY_METRICS_H__
#define __DISPLAY_METRICS_H__
namespace cdroid{
class DisplayMetrics{
public:
static constexpr int DENSITY_LOW = 120;
static constexpr int DENSITY_MEDIUM = 160;
static constexpr int DENSITY_TV = 213;
static constexpr int DENSITY_HIGH = 240;
static constexpr int DENSITY_XXHIGH = 480;
static constexpr int DENSITY_XXXHIGH= 640;
static constexpr int DENSITY_DEFAULT= DENSITY_MEDIUM;
static constexpr float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
static int DENSITY_DEVICE;
static int DENSITY_DEVICEE_STABLE;
public:
int widthPixels;
int heightPixels;
float density;
int densityDpi;
float scaledDensity;
float xdpi;
float ydpi;
int noncompatWidthPixels;
int noncompatHeightPixels;
float noncompatDensity;
int noncompatDensityDpi;
float noncompatScaledDensity;
float noncompatXdpi;
float noncompatYdpi;
public:
DisplayMetrics();
void setTo(const DisplayMetrics&);
void setToDefaults();
};
}
#endif

View File

@ -5,6 +5,7 @@
#include <drawables/colordrawable.h>
#include <drawables/bitmapdrawable.h>
#include <drawables/ninepatchdrawable.h>
#include <drawables/paintdrawable.h>
#include <drawables/picturedrawable.h>
#include <drawables/gradientdrawable.h>
#include <drawables/transitiondrawable.h>

View File

@ -0,0 +1,26 @@
#include <drawables/paintdrawable.h>
namespace cdroid{
PaintDrawable::PaintDrawable(){
}
PaintDrawable::PaintDrawable(int color){
}
void PaintDrawable::setCornerRadius(float radius){
std::vector<int>radii;
if(radius>0)
for(int i=0;i<8;i++)radii.push_back((int)radius);
setCornerRadii(radii);
}
void PaintDrawable::setCornerRadii(const std::vector<int>& radii){
if(radii.size()==0){
if(getShape())setShape(nullptr);
}else{
Rect inset={0,0,0,0};
setShape(new RoundRectShape(radii,inset,std::vector<int>()));
}
}
}

View File

@ -0,0 +1,17 @@
#ifndef __PAINT_DRAWABLE_H__
#define __PAINT_DRAWABLE_H__
#include <drawables/shapedrawable.h>
namespace cdroid{
class PaintDrawable:public ShapeDrawable{
public:
PaintDrawable();
PaintDrawable(int color);
void setCornerRadius(float radius);
void setCornerRadii(const std::vector<int>& radius);
};
}
#endif

View File

@ -204,6 +204,9 @@ void ArcShape::draw(Canvas&canvas){
canvas.restore();
}
OvalShape::OvalShape():RectShape(){
}
void OvalShape::draw(Canvas&canvas){
Rect r=rect();
@ -239,53 +242,61 @@ Shape*RoundRectShape::clone()const{
}
void RoundRectShape::setRadius(int radius){
mOuterRadii.resize(8);
for(int i=0;i<8;i++)mOuterRadii[i]=radius;
}
void RoundRectShape::onResize(int w, int h){
RectShape::onResize(w,h);
Rect r=rect();
mInnerRect.set(r.left+mInset.left,r.top+mInset.top,
r.right()-mInset.left-mInset.width,r.bottom()-mInset.top-mInset.height);
r.right()-mInset.left-mInset.width,r.bottom()-mInset.top-mInset.height);
}
static void round2pts(const Rect&r,const int*radius,float*pts){
pts[0]=r.left+radius[0];
pts[1]=r.top+radius[1];
pts[2]=r.right()-radius[2];
pts[3]=r.top+radius[3];
pts[4]=r.right()-radius[4];
pts[5]=r.bottom()-radius[5];
pts[6]=r.left+radius[6];
pts[7]=r.bottom()-radius[7];
LOGD("rect(%d,%d,%d,%d) radius=%d,%d;%d,%d;%d,%d;%d,%d",r.left,r.top,r.right(),r.bottom(),
radius[0],radius[1],radius[2],radius[3],
radius[4],radius[5],radius[6],radius[7]);
}
void RoundRectShape::draw(Canvas&canvas){
const double degree = M_PI/180;
const Rect r=rect();
const int *radius=mOuterRadii.data();
const float pts[8]={
(float)r.left+radius[0] , (float)r.top+radius[1],
(float)r.left+mWidth-radius[2] , (float)r.top+radius[3],
(float)r.left+mWidth-radius[4] , (float)r.top+mHeight-radius[5],
(float)r.left+radius[6] , (float)r.top+mHeight-radius[7]
};
float pts[8];
float db=180;
canvas.begin_new_sub_path();
for(int i=0;i<8;i+=2){
canvas.translate(pts[i],pts[i+1]);
canvas.scale(1,(float)radius[i+1]/radius[i]);
canvas.arc(0,0,radius[i],db*degree,(db+90)*degree);
canvas.scale(1,(float)radius[i]/radius[i+1]);
canvas.translate(-pts[i],-pts[i+1]);
db+=90.;
std::vector<int>*radiuss[2]={&mOuterRadii,&mInnerRadii};
Rect rects[2];
rects[0]=rects[1]=rect();
rects[1].inflate(-50,-50);//-mStrokeWidth,-mStrokeWidth);
for(int ii=0;ii<2;ii++){
if(radiuss[ii]->size()==0){
Rect r=rects[ii];
canvas.rectangle(r.left,r.top,r.width,r.height);
}else{
round2pts(rects[ii],radiuss[ii]->data(),pts);
for(int i=0;i<8;i+=2){
canvas.translate(pts[i],pts[i+1]);
canvas.scale(1,(float)radius[i+1]/radius[i]);
canvas.arc(0,0,radius[i],db*degree,(db+90)*degree);
canvas.scale(1,(float)radius[i]/radius[i+1]);
canvas.translate(-pts[i],-pts[i+1]);
db+=90.f;
}canvas.line_to(pts[0]-radius[0],pts[1]);
}
}
canvas.line_to(pts[0]-radius[0],pts[1]);
canvas.close_path();
#if 0
radius=mInnerRadii.data();
canvas.begin_new_sub_path();
for(int i=0;i<8;i+=2){
canvas.translate(pts[i],pts[i+1]);
canvas.scale(1,(float)radius[i+1]/radius[i]);
canvas.arc(0,0,radius[i],db*degree,(db+90)*degree);
canvas.scale(1,(float)radius[i]/radius[i+1]);
canvas.translate(-pts[i],-pts[i+1]);
db+=90.;
}
canvas.line_to(pts[0]-radius[0],pts[1]);
canvas.close_path();
#endif
canvas.set_fill_rule(Cairo::Context::FillRule::EVEN_ODD);//WINDING);//EVEN_ODD
fill_stroke(canvas);
}
}

View File

@ -72,6 +72,7 @@ public:
class OvalShape:public RectShape{
public:
OvalShape();
void draw(Canvas&canvas)override;
};

View File

@ -51,6 +51,8 @@ std::shared_ptr<Drawable::ConstantState>ShapeDrawable::getConstantState(){
}
void ShapeDrawable::setShape(Shape*shape){
if(mShapeState->mShape)
delete mShapeState->mShape;
mShapeState->mShape=shape;
updateShape();
}