Shape - "Hello,Shape"

 

 


/*
 *  MOUSE
 *  position x      : length
 *  position y      : thickness and number of lines
 *  drag            : draw
 */


ofColor col;
bool bClear = false;

//--------------------------------------------------------------
void testApp::setup(){
    
    ofSetWindowShape(720, 720);

    ofNoFill();
    ofBackground(255);
    
    ofSetBackgroundAuto(false);
    
    //
    col = ofColor(0);
}

//--------------------------------------------------------------
void testApp::update(){
    

}

//--------------------------------------------------------------
void testApp::draw(){
    
    // stupid method, fixed clear problem in mac
    if ( bClear ){
        ofSetBackgroundAuto(true);
        ofClear(255);
        bClear = false;
    }
    else
        ofSetBackgroundAuto(false);
    
    ofEnableAlphaBlending();

    if ( ofGetMousePressed()){
        ofPushMatrix();
        ofTranslate(ofGetWidth()*.5, ofGetHeight()*.5);
        
        int circle_res = ofMap(mouseX+100, 0, ofGetHeight(), 2, 10);
        float radius = mouseX - ofGetWidth()*.5 + 0.5;
        float angle = TWO_PI/circle_res;
        
        ofSetColor(col, 5);
        
        ofEnableSmoothing();
        ofBeginShape();
        for (int i = 0; i<=circle_res; i++) {
            float x = 0 + cos(angle*i) * radius;
            float y = 0 + sin(angle*i) * radius;
            ofVertex(x, y);
        }
        ofEndShape();
        ofDisableSmoothing();
        
        ofPopMatrix();
    }

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    
    if ( key == OF_KEY_BACKSPACE )  bClear = true;
    if ( key == 's' )               ofSaveScreen("C.png");
    if ( key == 'r' )               
        col = ofColor(ofRandomuf()*255, ofRandomuf()*255, ofRandomuf()*255, 10);
}

-GENERATIVE DESIGN-


在Android开发中,Shape可用于定义各种形状和图片资源,使用它能减少资源占用、缩小安装包大小,还可适配不同尺寸手机[^1]。 ### 使用方法 在res/drawable目录下创建一个XML文件来定义Shape。例如,创建一个名为`custom_shape.xml`的文件: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 形状类型,这里是矩形 --> <solid android:color="#FF0000" /> <!-- 填充颜色 --> <stroke android:width="2dp" android:color="#000000" /> <!-- 边框宽度和颜色 --> <corners android:radius="10dp" /> <!-- 圆角半径 --> <size android:width="200dp" android:height="100dp" /> <!-- 大小 --> </shape> ``` 然后在布局文件中使用这个Shape作为视图的背景: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_shape" android:text="Hello, Shape!" /> ``` ### 相关属性 - **形状类型(`android:shape`)**:取值有`rectangle`(矩形)、`oval`(椭圆)、`line`(线)、`ring`(圆环)。 - **填充颜色(`solid`)**:`<solid android:color="#颜色值" />`用于设置形状的填充颜色。 - **边框(`stroke`)**: - `android:width`:边框宽度。 - `android:color`:边框颜色。 - 还可以设置`android:dashWidth`和`android:dashGap`来创建虚线边框。 - **圆角(`corners`)**: - `android:radius`:统一设置四个角的圆角半径。 - 也可以分别设置每个角的半径,如`android:topLeftRadius`等。 - **大小(`size`)**:`android:width`和`android:height`用于设置Shape的大小。 若在XML里已将`BackGround`设为Shape属性,在代码中设置`BackGround`会覆盖Shape属性。若要保证Shape属性不变又能切换颜色,可定义多个`shape.xml`文件来实现[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值